Skip to content

Commit

Permalink
Merge pull request #78 from sparcs-kaist/migration@utils
Browse files Browse the repository at this point in the history
migrate util functions
  • Loading branch information
useruseruse authored Sep 18, 2024
2 parents 6076b84 + 3cb4ddd commit a1c4cb9
Show file tree
Hide file tree
Showing 20 changed files with 418 additions and 309 deletions.
80 changes: 42 additions & 38 deletions src/shapes/enum.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const enum AdditionalTrackType {
DOUBLE,
MINOR,
ADVANCED,
INTERDISCIPLINARY,
DOUBLE = 'DOUBLE', // 복수전공
MINOR = 'MINOR', // 부전공
ADVANCED = 'ADVANCED', // 심화전공
INTERDISCIPLINARY = 'INTERDISCIPLINARY', // 융합전공
}

export const enum SemesterType {
Expand All @@ -13,32 +13,31 @@ export const enum SemesterType {
}

export const enum ItemFocusFrom {
NONE,
LIST,
ADDING,
TABLE_TAKEN,
TABLE_FUTURE,
TABLE_ARBITRARY,
CATEGORY,
NONE = 'NONE',
LIST = 'LIST',
ADDING = 'ADDING',
TABLE_TAKEN = 'TABLE_TAKEN',
TABLE_FUTURE = 'TABLE_FUTURE',
TABLE_ARBITRARY = 'TABLE_ARBITRARY',
CATEGORY = 'CATEGORY',
}

export const enum LectureFocusFrom {
NONE,
LIST,
TABLE,
MULTIPLE,
NONE = 'NONE',
LIST = 'LIST',
TABLE = 'TABLE',
MULTIPLE = 'MULTIPLE',
}

export const enum ReviewsFocusFrom {
NONE,
LECTURE,
REVIEWS_LATEST,
REVIEWS_MY,
REVIEWS_LIKED,
REVIEWS_RANKED,
NONE = 'NONE',
LECTURE = 'LECTURE',
REVIEWS_LATEST = 'REVIEWS_LATEST',
REVIEWS_MY = 'REVIEWS_MY',
REVIEWS_LIKED = 'REVIEWS_LIKED',
REVIEWS_RANKED = 'REVIEWS_RANKED',
}

// string enum for PlannerSubSection.jsx
export const enum PlannerItemType {
TAKEN = 'TAKEN',
FUTURE = 'FUTURE',
Expand All @@ -55,32 +54,32 @@ export const enum CategoryFirstIndex {
}

export const enum CourseListCode {
SEARCH,
BASIC,
HUMANITY,
TAKEN,
SEARCH = 'SEARCH',
BASIC = 'BASIC',
HUMANITY = 'HUMANITY',
TAKEN = 'TAKEN',
}

export const enum LectureListCode {
SEARCH,
BASIC,
HUMANITY,
CART,
SEARCH = 'SEARCH',
BASIC = 'BASIC',
HUMANITY = 'HUMANITY',
CART = 'CART',
}

export const enum Day {
MON,
TUE,
WED,
THU,
FRI,
SAT,
SUN,
MON = 'MON',
TUE = 'TUE',
WED = 'WED',
THU = 'THU',
FRI = 'FRI',
SAT = 'SAT',
SUN = 'SUN',
}

/** Redux state 중에서 서버에서 course 또는 lecture를 fetch 해서 저장하는
* planner.list , timetable.list , dictionary.list 에서
아래 enum 을 optional key 로 사용합니다.*/
아래 enum 을 optional key 로 사용합니다.*/
export const enum DepartmentCode {
ALL = 'ALL',
HSS = 'HSS',
Expand All @@ -105,3 +104,8 @@ export const enum DepartmentCode {
BCS = 'BCS',
ETC = 'ETC',
}

export const enum Orientation {
HORIZONTAL = 'HORIZONTAL',
VERTICAL = 'VERTICAL',
}
13 changes: 8 additions & 5 deletions src/shapes/state/planner/ItemFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ export interface NoneItem {
export interface ListItem {
from: ItemFocusFrom.LIST;
clicked: boolean;
item?: null;
item: null;
course?: Course | ArbitraryPseudoCourse;
category?: null;
category: null;
reviews?: Review[];
lectures?: Lecture[];
}

export interface AddingItem {
from: ItemFocusFrom.ADDING;
clicked: true;
item?: null;
item: null;
course?: Course | ArbitraryPseudoCourse;
category?: null;
reviews?: Review[];
Expand All @@ -53,7 +53,7 @@ export interface AddingItem {
export interface TableItem {
from: ItemFocusFrom.TABLE_TAKEN | ItemFocusFrom.TABLE_FUTURE | ItemFocusFrom.TABLE_ARBITRARY;
clicked: boolean;
item?: TakenPlannerItem | FuturePlannerItem | ArbitraryPlannerItem;
item: TakenPlannerItem | FuturePlannerItem | ArbitraryPlannerItem;
course?: Course | ArbitraryPseudoCourse;
category?: null;
reviews?: Review[];
Expand All @@ -63,13 +63,16 @@ export interface TableItem {
export interface CategoryItem {
from: ItemFocusFrom.CATEGORY;
clicked: boolean;
item?: null;
item: null;
course?: null;
category: number[];
reviews?: null;
lectures?: null;
}

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

type ItemFocus = NoneItem | ListItem | AddingItem | TableItem | CategoryItem;

export default ItemFocus;
12 changes: 6 additions & 6 deletions src/shapes/state/timetable/LectureFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ export interface Detail {
interface FromNone {
from: LectureFocusFrom.NONE;
clicked: false;
lecture?: null;
reviews?: null;
lecture: null;
reviews: null;
multipleTitle: '';
multipleDetails: EmtpyArray;
}

interface FromListOrTable {
from: LectureFocusFrom.LIST | LectureFocusFrom.TABLE;
clicked: boolean;
lecture?: Lecture;
reviews?: Review[];
lecture: Lecture;
reviews: Review[];
multipleTitle: '';
multipleDetails: EmtpyArray;
}

interface FromMutliple {
from: LectureFocusFrom.MULTIPLE;
clicked: false;
lecture?: null;
reviews?: null;
lecture: null;
reviews: null;
multipleTitle: '';
multipleDetails: Detail[];
}
Expand Down
19 changes: 7 additions & 12 deletions src/shapes/state/timetable/LectureLists.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import type Lecture from '@/shapes/model/subject/Lecture';

interface LectureGroups {
lectureGroups: Lecture[][];
}
export default interface LectureLists {
search: {
lectureGroups: Lecture[][];
};
basic: {
lectureGroups: Lecture[][];
};
humanity: {
lectureGroups: Lecture[][];
};
cart: {
lectureGroups: Lecture[][];
};
search: LectureGroups;
basic: LectureGroups;
humanity: LectureGroups;
cart: LectureGroups;
}
6 changes: 4 additions & 2 deletions src/utils/commonUtils.jsx → src/utils/commonUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react';

export const unique = (array, compareFunction = undefined) => {
type ComapreFunc = <T>(v1: T, v2: T) => number;

export const unique = <T,>(array: T[], compareFunction: ComapreFunc) => {
if (!compareFunction) {
return Array.from(new Set(array));
}
return array.filter((v, i) => array.findIndex((v2) => compareFunction(v, v2)) === i);
};

export const formatNewlineToBr = (content) => {
export const formatNewlineToBr = (content: string) => {
const contentLines = content.split('\n');
return contentLines
.map((l, i) => ({
Expand Down
21 changes: 0 additions & 21 deletions src/utils/courseUtils.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/utils/courseUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Course from '@/shapes/model/subject/Course';
import User from '@/shapes/model/session/User';
import CourseFocus from '@/shapes/state/dictionary/CourseFocus';
import { getTranslatedString } from '@/utils/translationUtils';

//Dictionary Page 에서 사용되는 유틸들입니다.

// Dictionary Page 의 Course List Section 에서 포커싱된 Course가 현재 Course이면 True를 반환합니다.
export const isFocused = (course: Course, courseFocus: CourseFocus) =>
Boolean(courseFocus.course) && courseFocus.course?.id === course.id;

// Dictionary Page 의 Course List Section 에서 포커싱된 Course가 있고, 포커싱된 Course가 현재 Course가 아니면 True를 반환합니다.
export const isDimmedCourse = (course: Course, courseFocus: CourseFocus) =>
Boolean(courseFocus.course) && courseFocus.course?.id !== course.id;

// Dictionary Page의 Course List Section에서 이미 수강한 Course 이면 True를 반환합니다.
// isTaken 이 True 이면 리뷰 작성하기 블록이 보여집니다.
export const isTaken = (courseId: number, user: User) =>
user.review_writable_lectures.some((l) => l.course === courseId);

// Dictionary Page의 Course Block 에서 교수님들의 이름 리스트를 가져오기 위해 사용합니다.
export const getProfessorsFullStr = (course: Course) => {
const professors = course.professors.slice().sort((a, b) => (a.name < b.name ? -1 : 1));
const professorNames = professors.map((p) => getTranslatedString(p, 'name'));
return professorNames.join(', ');
};

export const isSpecialLectureCourse = (course: Course) =>
course.title.includes('특강') ||
course.title_en.includes('Special Lectures') ||
course.title_en.includes('Special Topics');
8 changes: 0 additions & 8 deletions src/utils/examtimeUtils.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/utils/examtimeUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Examtime from '@/shapes/model/subject/Examtime';
import { getTranslatedString } from '@/utils/translationUtils';

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

export const getTimeStr = (examtime: Examtime) => {
const fullStr = getStr(examtime);
return fullStr?.slice(fullStr.indexOf(' '));
};
Loading

0 comments on commit a1c4cb9

Please sign in to comment.