Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate redux reducer about dictionary #64

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
sboh1214 marked this conversation as resolved.
Show resolved Hide resolved
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[];
sboh1214 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading