Skip to content

Commit

Permalink
fix: migrate utils
Browse files Browse the repository at this point in the history
  • Loading branch information
useruseruse committed Jul 24, 2024
1 parent 6076b84 commit 1d5ed49
Show file tree
Hide file tree
Showing 12 changed files with 563 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/shapes/state/dictionary/CourseFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type Course from '@/shapes/model/subject/Course';
import type Lecture from '@/shapes/model/subject/Lecture';
import type Review from '@/shapes/model/review/Review';

interface NullCourseFocus {
export interface NullCourseFocus {
course: null;
reviews: null;
lectures: null;
}

interface FullCourseFocus {
export interface FullCourseFocus {
course: Course;
reviews: Review[];
lectures: Lecture[];
Expand Down
23 changes: 13 additions & 10 deletions src/shapes/state/planner/ItemFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ export interface ArbitraryPseudoCourse {
old_code: string;
}

export type PlannerCourse = Course | ArbitraryPseudoCourse;
export type PlannerItem = TakenPlannerItem | FuturePlannerItem | ArbitraryPlannerItem;

export interface NoneItem {
from: ItemFocusFrom.NONE;
clicked: false;
item?: null;
course?: null;
item: null;
course: null;
category?: null;
reviews?: null;
lectures?: null;
Expand All @@ -33,8 +36,8 @@ export interface NoneItem {
export interface ListItem {
from: ItemFocusFrom.LIST;
clicked: boolean;
item?: null;
course?: Course | ArbitraryPseudoCourse;
item: null;
course: Course | ArbitraryPseudoCourse;
category?: null;
reviews?: Review[];
lectures?: Lecture[];
Expand All @@ -43,8 +46,8 @@ export interface ListItem {
export interface AddingItem {
from: ItemFocusFrom.ADDING;
clicked: true;
item?: null;
course?: Course | ArbitraryPseudoCourse;
item: null;
course: Course | ArbitraryPseudoCourse;
category?: null;
reviews?: Review[];
lectures?: Lecture[];
Expand All @@ -53,8 +56,8 @@ export interface AddingItem {
export interface TableItem {
from: ItemFocusFrom.TABLE_TAKEN | ItemFocusFrom.TABLE_FUTURE | ItemFocusFrom.TABLE_ARBITRARY;
clicked: boolean;
item?: TakenPlannerItem | FuturePlannerItem | ArbitraryPlannerItem;
course?: Course | ArbitraryPseudoCourse;
item: TakenPlannerItem | FuturePlannerItem | ArbitraryPlannerItem;
course: Course | ArbitraryPseudoCourse;
category?: null;
reviews?: Review[];
lecture?: Lecture[];
Expand All @@ -63,8 +66,8 @@ export interface TableItem {
export interface CategoryItem {
from: ItemFocusFrom.CATEGORY;
clicked: boolean;
item?: null;
course?: null;
item: null;
course: null;
category: number[];
reviews?: null;
lectures?: null;
Expand Down
6 changes: 3 additions & 3 deletions src/shapes/state/timetable/LectureFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Detail {
info: string;
}

interface FromNone {
export interface FromNone {
from: LectureFocusFrom.NONE;
clicked: false;
lecture?: null;
Expand All @@ -18,7 +18,7 @@ interface FromNone {
multipleDetails: EmtpyArray;
}

interface FromListOrTable {
export interface FromListOrTable {
from: LectureFocusFrom.LIST | LectureFocusFrom.TABLE;
clicked: boolean;
lecture?: Lecture;
Expand All @@ -27,7 +27,7 @@ interface FromListOrTable {
multipleDetails: EmtpyArray;
}

interface FromMutliple {
export interface FromMutliple {
from: LectureFocusFrom.MULTIPLE;
clicked: false;
lecture?: null;
Expand Down
26 changes: 26 additions & 0 deletions src/utils/courseUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useTranslatedString } from './../hooks/useTranslatedString';

Check warning on line 1 in src/utils/courseUtil.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/courseUtil.ts#L1

Added line #L1 was not covered by tests
import i18n from 'i18next';
import Course from '@/shapes/model/subject/Course';
import { FullCourseFocus } from '@/shapes/state/dictionary/CourseFocus';
import User from '@/shapes/model/session/User';

export const isFocused = (course: Course, courseFocus: FullCourseFocus) =>

Check warning on line 7 in src/utils/courseUtil.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/courseUtil.ts#L7

Added line #L7 was not covered by tests
Boolean(courseFocus.course) && courseFocus.course.id === course.id;

export const isDimmedCourse = (course: Course, courseFocus: FullCourseFocus) =>

Check warning on line 10 in src/utils/courseUtil.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/courseUtil.ts#L10

Added line #L10 was not covered by tests
Boolean(courseFocus.course) && courseFocus.course.id !== course.id;

export const isTaken = (courseId: number, user: User) =>
user.review_writable_lectures.some((l) => l.course === courseId);

Check warning on line 14 in src/utils/courseUtil.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/courseUtil.ts#L13-L14

Added lines #L13 - L14 were not covered by tests

export const getProfessorsFullStr = (course: Course) => {
const translate = useTranslatedString();

Check warning on line 17 in src/utils/courseUtil.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/courseUtil.ts#L16-L17

Added lines #L16 - L17 were not covered by tests
const professors = course.professors.slice().sort((a, b) => (a.name < b.name ? -1 : 1));
const professorNames = professors.map((p) => translate(p, 'name'));
return professorNames.join(', ');

Check warning on line 20 in src/utils/courseUtil.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/courseUtil.ts#L19-L20

Added lines #L19 - L20 were not covered by tests
};

export const isSpecialLectureCourse = (course: Course) =>

Check warning on line 23 in src/utils/courseUtil.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/courseUtil.ts#L23

Added line #L23 was not covered by tests
course.title.includes('특강') ||
course.title_en.includes('Special Lectures') ||
course.title_en.includes('Special Topics');

Check warning on line 26 in src/utils/courseUtil.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/courseUtil.ts#L25-L26

Added lines #L25 - L26 were not covered by tests
11 changes: 11 additions & 0 deletions src/utils/examtimeUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useTranslatedString } from '@/hooks/useTranslatedString';

Check warning on line 1 in src/utils/examtimeUtils.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/examtimeUtils.ts#L1

Added line #L1 was not covered by tests
import Examtime from '@/shapes/model/subject/Examtime';

const translate = useTranslatedString();

Check warning on line 4 in src/utils/examtimeUtils.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/examtimeUtils.ts#L4

Added line #L4 was not covered by tests

export const getStr = (examtime: Examtime) => translate(examtime, 'str');

Check warning on line 6 in src/utils/examtimeUtils.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/examtimeUtils.ts#L6

Added line #L6 was not covered by tests

export const getTimeStr = (examtime: Examtime) => {
const fullStr = getStr(examtime);

Check warning on line 9 in src/utils/examtimeUtils.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/examtimeUtils.ts#L8-L9

Added lines #L8 - L9 were not covered by tests
return fullStr?.slice(fullStr.indexOf(' '));
};
107 changes: 107 additions & 0 deletions src/utils/itemCategoryUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { CategoryFirstIndex } from '@/shapes/enum';
import Planner from '@/shapes/model/planner/Planner';
import { PlannerItem } from '@/shapes/state/planner/ItemFocus';
import { AdditionalTrackType } from '@/shapes/enum';

export const isIdenticalCategory = (category1, category2) =>
category1 &&
category2 &&
category1[0] === category2[0] &&
category1[1] === category2[1] &&
category1[2] === category2[2];

export const getSeparateMajorTracks = (planner: Planner) => {
if (!planner) {
return [];
}

return [
planner.major_track,
...planner.additional_tracks.filter((at) => at.type === AdditionalTrackType.DOUBLE),
...planner.additional_tracks.filter((at) => at.type === AdditionalTrackType.MINOR),
...planner.additional_tracks.filter((at) => at.type === AdditionalTrackType.INTERDISCIPLINARY),
];
};

export const getCategoryOfType = (planner: Planner, type: string, departmentCode?: string) => {
switch (type) {
case 'Basic Required':
return [CategoryFirstIndex.BASIC, 0, 0];
case 'Basic Elective':
return [CategoryFirstIndex.BASIC, 0, 1];
case 'Major Required': {
const separateMajorTracks = getSeparateMajorTracks(planner);
const targetTrack =
separateMajorTracks.find((smt) => smt.department?.code === departmentCode) ||
separateMajorTracks.find((smt) => smt.type === 'INTERDISCIPLINARY');
if (targetTrack) {
const secondIndex = separateMajorTracks.findIndex((smt) => smt.id === targetTrack.id);
return [CategoryFirstIndex.MAJOR, secondIndex, 0];
}
return [CategoryFirstIndex.OTHERS, 0, 0];
}
case 'Major Elective':
case 'Elective(Graduate)': {
const separateMajorTracks = getSeparateMajorTracks(planner);
const targetTrack =
separateMajorTracks.find((smt) => smt.department?.code === departmentCode) ||
separateMajorTracks.find((smt) => smt.type === AdditionalTrackType.INTERDISCIPLINARY);
if (targetTrack) {
const secondIndex = separateMajorTracks.findIndex((smt) => smt.id === targetTrack.id);
return [CategoryFirstIndex.MAJOR, secondIndex, 1];
}
return [CategoryFirstIndex.OTHERS, 0, 0];
}
case 'Thesis Study(Undergraduate)':
return [CategoryFirstIndex.RESEARCH, 0, 0];
case 'Individual Study':
return [CategoryFirstIndex.RESEARCH, 0, 1];
case 'General Required':
case 'Mandatory General Courses':
return [CategoryFirstIndex.GENERAL_AND_HUMANITY, 0, 0];
case 'Humanities & Social Elective':
return [CategoryFirstIndex.GENERAL_AND_HUMANITY, 0, 1];
case 'Other Elective':
return [CategoryFirstIndex.OTHERS, 0, 0];
default:
break;
}
if (type?.startsWith('Humanities & Social Elective')) {
return [CategoryFirstIndex.GENERAL_AND_HUMANITY, 0, 1];
}
return [CategoryFirstIndex.OTHERS, 0, 0];
};

export const getCategoryOfItem = (planner: Planner, item: PlannerItem) => {
switch (item.item_type) {
case 'TAKEN':
return getCategoryOfType(planner, item.lecture.type_en, item.lecture.department_code);
case 'FUTURE':
return getCategoryOfType(planner, item.course.type_en, item.course.department?.code);
case 'ARBITRARY':
return getCategoryOfType(planner, item.type_en, item.department?.code);
default:
return getCategoryOfType(planner, '', '');
}
};

export const getColorOfCategory = (planner: Planner, category) => {
switch (category[0]) {
case 0:
return 1;
case 1:
return 3 + ((category[1] * 2) % 7);
case 2:
return 11;
case 3:
return 14;
case 4:
return 17;
default:
return 17;
}
};

export const getColorOfItem = (planner: Planner, item: PlannerItem) => {
return getColorOfCategory(planner, getCategoryOfItem(planner, item));
};
90 changes: 90 additions & 0 deletions src/utils/itemFocusUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { ItemFocusFrom } from '@/shapes/enum';
import {
getCategoryOfItem,
getSeparateMajorTracks,
isIdenticalCategory,
} from './itemCategoryUtils';
import { CategoryFirstIndex } from '@/shapes/enum';
import { getCourseOfItem, getCreditOfItem, getAuOfItem } from './itemUtils';
import ItemFocus, { PlannerItem, PlannerCourse } from '@/shapes/state/planner/ItemFocus';
import Planner from '@/shapes/model/planner/Planner';

export const isIdenticalItem = (item1: PlannerItem, item2: PlannerItem) =>
item1 != null && item2 != null && item1.item_type === item2.item_type && item1.id === item2.id;

export const isTableFocusedItem = (item: PlannerItem, itemFocus: ItemFocus) =>
(itemFocus.from === ItemFocusFrom.TABLE_TAKEN ||
itemFocus.from === ItemFocusFrom.TABLE_FUTURE ||
itemFocus.from === ItemFocusFrom.TABLE_ARBITRARY) &&
isIdenticalItem(item, itemFocus.item);

export const isTableClickedItem = (item: PlannerItem, itemFocus: ItemFocus) =>
isTableFocusedItem(item, itemFocus) && itemFocus.clicked === true;

export const isSingleFocusedItem = (item: PlannerItem, itemFocus: ItemFocus) =>
isTableFocusedItem(item, itemFocus) ||
((itemFocus.from === ItemFocusFrom.LIST || itemFocus.from === ItemFocusFrom.ADDING) &&
getCourseOfItem(item) &&
itemFocus.course &&
getCourseOfItem(item).id === itemFocus.course.id);

export const isMultipleFocusedItem = (
item: PlannerItem,
itemFocus: ItemFocus,
planner: Planner,
) => {
if (itemFocus.from !== ItemFocusFrom.CATEGORY) {
return false;
}
if (!planner) {
return false;
}
const focusedCategory = itemFocus.category;
const itemCategory = getCategoryOfItem(planner, item);
if (focusedCategory[0] === CategoryFirstIndex.TOTAL) {
if (focusedCategory[2] === 0) {
return getCreditOfItem(item) > 0;
}
return getAuOfItem(item) > 0;
}
if (focusedCategory[0] !== itemCategory[0]) {
return false;
}
switch (focusedCategory[0]) {
case CategoryFirstIndex.MAJOR: {
const targetSmt = getSeparateMajorTracks(planner)[focusedCategory[1]];
if (targetSmt.major_required === 0) {
if (focusedCategory[2] === 0) {
return false;
}
const mrCategory = [focusedCategory[0], focusedCategory[1], 0];
return (
isIdenticalCategory(itemCategory, focusedCategory) ||
isIdenticalCategory(itemCategory, mrCategory)
);
}
return isIdenticalCategory(itemCategory, focusedCategory);
}
default: {
return isIdenticalCategory(itemCategory, focusedCategory);
}
}
};

export const isFocusedItem = (item: PlannerItem, itemFocus: ItemFocus, planner?: Planner) =>
isSingleFocusedItem(item, itemFocus) || isMultipleFocusedItem(item, itemFocus, planner);

export const isDimmedItem = (item: PlannerItem, itemFocus: ItemFocus) =>
!isFocusedItem(item, itemFocus) && itemFocus.clicked === true;

export const isFocusedListCourse = (course: PlannerCourse, itemFocus: ItemFocus) =>
itemFocus.from === ItemFocusFrom.LIST && itemFocus.course.id === course.id;

export const isClickedListCourse = (course: PlannerCourse, itemFocus: ItemFocus) =>
itemFocus.from === ItemFocusFrom.LIST &&
itemFocus.course.id === course.id &&
itemFocus.clicked === true;

export const isDimmedListCourse = (course: PlannerCourse, itemFocus: ItemFocus) =>
itemFocus.clicked === true &&
(itemFocus.course?.id !== course.id || itemFocus.from !== ItemFocusFrom.LIST);
Loading

0 comments on commit 1d5ed49

Please sign in to comment.