Skip to content

Commit 4e18f38

Browse files
leslieyip02zwliew
andauthored
fix(website/ta): Prevent duplicate TA lesson in reducer (#3940)
Co-authored-by: Zhao Wei Liew <[email protected]>
1 parent 4efafed commit 4e18f38

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

website/src/reducers/timetables.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ describe('TA module reducer', () => {
171171
},
172172
});
173173
});
174+
175+
test('should not add duplicate TA lessons', () => {
176+
expect(
177+
reducer(withTaModules, addTaLessonInTimetable(1, 'CS1010S', 'Tutorial', '1')),
178+
).toMatchObject({
179+
ta: {
180+
[1]: { CS1010S: [['Tutorial', '1']] },
181+
[2]: { CS1010S: [['Tutorial', '1']] },
182+
},
183+
});
184+
});
174185
});
175186

176187
describe('lesson reducer', () => {

website/src/reducers/timetables.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { produce } from 'immer';
33
import { createMigrate } from 'redux-persist';
44

55
import { PersistConfig } from 'storage/persistReducer';
6-
import { ModuleCode } from 'types/modules';
6+
import { ClassNo, LessonType, ModuleCode } from 'types/modules';
77
import { ModuleLessonConfig, SemTimetableConfig, TaModulesConfig } from 'types/timetables';
88
import { ColorMapping, TimetablesState } from 'types/reducers';
99

@@ -192,9 +192,13 @@ function semTaModules(state = DEFAULT_TA_STATE, action: Actions): TaModulesConfi
192192
case ADD_TA_LESSON_IN_TIMETABLE: {
193193
const { moduleCode, lessonType, classNo } = action.payload;
194194
if (!(moduleCode && lessonType && classNo)) return state;
195+
const newLesson: [LessonType, ClassNo] = [lessonType, classNo];
196+
const curLessons = state[moduleCode] ?? [];
197+
// Prevent duplicate lessons
198+
if (curLessons.some((lesson) => isEqual(lesson, newLesson))) return state;
195199
return {
196200
...state,
197-
[moduleCode]: [...(state[moduleCode] ?? []), [lessonType, classNo]],
201+
[moduleCode]: [...curLessons, newLesson],
198202
};
199203
}
200204
case REMOVE_TA_LESSON_IN_TIMETABLE: {

0 commit comments

Comments
 (0)