From 60a3650bda01a444f0b0b28fabbb31fabefa13c0 Mon Sep 17 00:00:00 2001 From: Zhao Wei Liew Date: Fri, 4 Aug 2023 17:54:44 +0800 Subject: [PATCH] timetable: fix more nullability issues --- website/src/actions/timetables.test.ts | 6 ++++-- website/src/actions/timetables.ts | 11 +++-------- website/src/types/reducers.ts | 2 +- website/src/views/timetable/TimetableContent.tsx | 3 ++- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/website/src/actions/timetables.test.ts b/website/src/actions/timetables.test.ts index 8d253ab2829..87f78daccd3 100644 --- a/website/src/actions/timetables.test.ts +++ b/website/src/actions/timetables.test.ts @@ -53,12 +53,13 @@ describe('fillTimetableBlanks', () => { const moduleBank = { modules: { CS1010S, CS3216 } }; const timetablesState = (semester: Semester, timetable: SemTimetableConfig) => ({ lessons: { [semester]: timetable }, + customisedModules: { [semester]: [] }, }); const semester = 1; const action = actions.validateTimetable(semester); test('do nothing if timetable is already full', () => { - const timetable = { + const timetable: SemTimetableConfig = { CS1010S: { Lecture: ['1'], Tutorial: ['1'], @@ -66,6 +67,7 @@ describe('fillTimetableBlanks', () => { }, }; + // TODO(zwliew): Correctly type all the `state: any` declarations in this function and the rest of the codebase. const state: any = { timetables: timetablesState(semester, timetable), moduleBank }; const dispatch = jest.fn(); action(dispatch, () => state); @@ -74,7 +76,7 @@ describe('fillTimetableBlanks', () => { }); test('fill missing lessons with randomly generated modules', () => { - const timetable = { + const timetable: SemTimetableConfig = { CS1010S: { Lecture: ['1'], Tutorial: ['1'], diff --git a/website/src/actions/timetables.ts b/website/src/actions/timetables.ts index c1a73c833cb..ce7763badc9 100644 --- a/website/src/actions/timetables.ts +++ b/website/src/actions/timetables.ts @@ -236,14 +236,9 @@ export function validateTimetable(semester: Semester) { const module = moduleBank.modules[moduleCode]; if (!module) return; - // If the module is customised, we do not validate it - if ( - timetables.customisedModules && - timetables.customisedModules[semester] && - timetables.customisedModules[semester].includes(moduleCode) - ) { - return; - } + // Do not validate customised modules. + if (timetables.customisedModules[semester]?.includes(moduleCode)) return; + const [validatedLessonConfig, changedLessonTypes] = validateModuleLessons( semester, lessonConfig, diff --git a/website/src/types/reducers.ts b/website/src/types/reducers.ts index 406c7bc6585..24bb6b73898 100644 --- a/website/src/types/reducers.ts +++ b/website/src/types/reducers.ts @@ -113,7 +113,7 @@ export type SettingsState = { export type ColorMapping = { [moduleCode: string]: ColorIndex }; export type SemesterColorMap = { [semester: string]: ColorMapping }; export type HiddenModulesMap = { [semester: string]: ModuleCode[] }; -export type CustomisedModulesMap = { [semester: string]: ModuleCode[] }; +export type CustomisedModulesMap = { [semester: string]: ModuleCode[] | undefined }; export type TimetablesState = { readonly lessons: TimetableConfig; diff --git a/website/src/views/timetable/TimetableContent.tsx b/website/src/views/timetable/TimetableContent.tsx index cfb8039146d..462a3ebe5c7 100644 --- a/website/src/views/timetable/TimetableContent.tsx +++ b/website/src/views/timetable/TimetableContent.tsx @@ -509,6 +509,7 @@ function mapStateToProps(state: StoreState, ownProps: OwnProps) { const { semester, timetable } = ownProps; const { modules } = state.moduleBank; const timetableWithLessons = hydrateSemTimetableWithLessons(timetable, modules, semester); + // TODO(zwliew): fix the type signature of state.timetables.hidden[semester] const hiddenInTimetable = state.timetables.hidden[semester] || []; return { @@ -518,7 +519,7 @@ function mapStateToProps(state: StoreState, ownProps: OwnProps) { modules, activeLesson: state.app.activeLesson, customiseModule: state.app.customiseModule, - customisedModules: state.timetables.customisedModules[semester], + customisedModules: state.timetables.customisedModules[semester] ?? [], timetableOrientation: state.theme.timetableOrientation, showTitle: state.theme.showTitle, hiddenInTimetable,