Skip to content

Commit

Permalink
Merge pull request #64 from sparcs-kaist/migration@reducers/dictionary
Browse files Browse the repository at this point in the history
Migrate redux reducer about dictionary
  • Loading branch information
sboh1214 authored May 13, 2024
2 parents c729e98 + 8121398 commit d0db360
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
import Course from '@/shapes/model/subject/Course';
import {
RESET,
SET_COURSE_FOCUS,
CLEAR_COURSE_FOCUS,
SET_REVIEWS,
UPDATE_REVIEW,
SET_LECTURES,
} from '../../actions/dictionary/courseFocus';
DictionaryAction,
} from '@/actions/dictionary/courseFocus';
import Lecture from '@/shapes/model/subject/Lecture';
import Review from '@/shapes/model/review/Review';

const initialState = {
interface CourseFocusState {
course: Course | null;
lectures: Lecture[] | null;
reviews: Review[] | null;
}

const initialState: CourseFocusState = {
course: null,
reviews: null,
lectures: null,
};

const courseFocus = (state = initialState, action) => {
const courseFocus = (state = initialState, action: DictionaryAction) => {
switch (action.type) {
case RESET: {
return initialState;
}
case SET_COURSE_FOCUS: {
const courseChanged = !state.course || state.course.id !== action.course.id;
return Object.assign(
{},
state,
{
course: action.course,
},
courseChanged ? { reviews: null, lectures: null } : {},
);

return {
...state,
course: action.course,
reviews: courseChanged ? null : state.reviews,
lectures: courseChanged ? null : state.lectures,
};
}
case CLEAR_COURSE_FOCUS: {
return Object.assign({}, state, {
course: null,
reviews: null,
lectures: null,
});
return { ...state, course: null, reviews: null, lectures: null };
}
case SET_REVIEWS: {
return Object.assign({}, state, {
reviews: action.reviews,
});
return { ...state, reviews: action.reviews };
}
case UPDATE_REVIEW: {
const originalReviews = state.reviews;

if (!originalReviews) {
return state;
}

const { review, isNew } = action;
const foundIndex = originalReviews.findIndex((r) => r.id === review.id);
const newReviews =
Expand All @@ -55,14 +63,10 @@ const courseFocus = (state = initialState, action) => {
: isNew
? [review, ...originalReviews.slice()]
: [...originalReviews.slice()];
return Object.assign({}, state, {
reviews: newReviews,
});
return { ...state, reviews: newReviews };
}
case SET_LECTURES: {
return Object.assign({}, state, {
lectures: action.lectures,
});
return { ...state, lectures: action.lectures };
}
default: {
return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ const CombinedReducer = combineReducers({
search: search,
});

export type DictionaryState = ReturnType<typeof CombinedReducer>;
export default CombinedReducer;
66 changes: 0 additions & 66 deletions src/reducers/dictionary/list.js

This file was deleted.

86 changes: 86 additions & 0 deletions src/reducers/dictionary/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
RESET,
SET_SELECTED_LIST_CODE,
SET_LIST_COURSES,
CLEAR_SEARCH_LIST_COURSES,
ADD_COURSE_READ,
DictionaryAction,
} from '@/actions/dictionary/list';

import { CourseListCode } from '@/shapes/enum';
import Course from '@/shapes/model/subject/Course';

interface ListState {
selectedListCode: CourseListCode;
lists: {
[key in CourseListCode]: {
courses: Course[] | null;
};
};
readCourses: Course[];
}

const initialState: ListState = {
selectedListCode: CourseListCode.SEARCH,
lists: {
[CourseListCode.SEARCH]: {
courses: [],
},
[CourseListCode.BASIC]: {
courses: null,
},
[CourseListCode.HUMANITY]: {
courses: null,
},
[CourseListCode.TAKEN]: {
courses: null,
},
},
readCourses: [],
};

const list = (state = initialState, action: DictionaryAction) => {
switch (action.type) {
case RESET: {
return initialState;
}
case SET_SELECTED_LIST_CODE: {
return { ...state, selectedListCode: action.listCode };
}
case SET_LIST_COURSES: {
return {
...state,
lists: {
...state.lists,
[action.code]: {
...state.lists[action.code],
courses: action.courses,
},
},
};
}
case CLEAR_SEARCH_LIST_COURSES: {
return {
...state,
lists: {
...state.lists,
[CourseListCode.SEARCH]: {
...state.lists[CourseListCode.SEARCH],
courses: null,
},
},
};
}
case ADD_COURSE_READ: {
return {
...state,
readCourses: [...state.readCourses, action.course],
};
}
default: {
return state;
}
}
};

export default list;
42 changes: 0 additions & 42 deletions src/reducers/dictionary/search.js

This file was deleted.

40 changes: 40 additions & 0 deletions src/reducers/dictionary/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
RESET,
OPEN_SEARCH,
CLOSE_SEARCH,
SET_LAST_SEARCH_OPTION,
CourseAction,
} from '@/actions/dictionary/search';
import CourseLastSearchOption from '@/shapes/state/dictionary/CourseLastSearchOption';

interface SearchState {
open: boolean;
lastSearchOption: CourseLastSearchOption;
}

const initialState: SearchState = {
open: true,
lastSearchOption: {},
};

const search = (state = initialState, action: CourseAction) => {
switch (action.type) {
case RESET: {
return initialState;
}
case OPEN_SEARCH: {
return { ...state, open: true };
}
case CLOSE_SEARCH: {
return { ...state, open: false };
}
case SET_LAST_SEARCH_OPTION: {
return { ...state, lastSearchOption: action.lastSearchOption };
}
default: {
return state;
}
}
};

export default search;
10 changes: 5 additions & 5 deletions src/shapes/state/dictionary/CourseLastSearchOption.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default interface CourseLastSearchOption {
keyword: string;
type: string[];
department: string[];
grade: string[];
term: string[];
keyword?: string;
type?: string[];
department?: string[];
grade?: string[];
term?: string[];
}

0 comments on commit d0db360

Please sign in to comment.