Skip to content

Commit 74c53a9

Browse files
Fix modules in Special Term do not show up in Today page (#3814)
* exclude-lessons-not-current-week-from-today-schedule * Revert "exclude-lessons-not-current-week-from-today-schedule" This reverts commit c1a8574. * use sem2 weekInfo considering week before sem2 * update semesterNameMap in TodayContainer and add tests * use startOfDay for date in isLessonAvailable * fix linting errors * fix linting errors --------- Co-authored-by: Ravern Koh <[email protected]>
1 parent ea780cb commit 74c53a9

File tree

3 files changed

+138
-7
lines changed

3 files changed

+138
-7
lines changed

website/src/utils/timetables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
sample,
1919
values,
2020
} from 'lodash';
21-
import { addDays, min as minDate, parseISO } from 'date-fns';
21+
import { addDays, min as minDate, parseISO, startOfDay } from 'date-fns';
2222
import qs from 'query-string';
2323

2424
import {
@@ -360,7 +360,7 @@ export function isLessonAvailable(
360360
(weekRange) => {
361361
const end = minDate([parseISO(weekRange.end), date]);
362362
for (let current = parseISO(weekRange.start); current <= end; current = addDays(current, 7)) {
363-
if (isEqual(current, date)) return true;
363+
if (isEqual(current, startOfDay(date))) return true;
364364
}
365365

366366
return false;

website/src/views/today/TodayContainer/TodayContainer.test.tsx

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe(TodayContainerComponent, () => {
251251
});
252252

253253
describe(mapStateToProps, () => {
254-
test('should use correct semester', () => {
254+
test('should use correct semester (test 1, special case)', () => {
255255
// On week -1 of sem 2 the semester should be 2, not 1
256256
const ownProps: any = {
257257
// Week -1 of sem 2 of AY2018/2019
@@ -280,3 +280,123 @@ describe(mapStateToProps, () => {
280280
expect(mapStateToProps(state, ownProps).timetableWithLessons).toHaveProperty('CS1010S');
281281
});
282282
});
283+
284+
describe(mapStateToProps, () => {
285+
test('should use correct semester (test 2)', () => {
286+
// On week -1 of orientation week, it should be special term II
287+
const ownProps: any = {
288+
currentTime: new Date('2024-08-04T00:00:00.000Z'),
289+
};
290+
291+
const state: any = {
292+
moduleBank: { modules: {} },
293+
timetables: {
294+
lessons: {
295+
[4]: {
296+
CS3216: {},
297+
},
298+
[1]: {
299+
CS1010S: {},
300+
},
301+
},
302+
colors: {
303+
[4]: COLORS,
304+
[1]: COLORS,
305+
},
306+
},
307+
};
308+
309+
// Should return special term II timetable, not sem 1
310+
expect(mapStateToProps(state, ownProps).timetableWithLessons).toHaveProperty('CS3216');
311+
});
312+
});
313+
314+
describe(mapStateToProps, () => {
315+
test('should use correct semester (test 3)', () => {
316+
// On orientation week, it should be sem1
317+
const ownProps: any = {
318+
currentTime: new Date('2024-08-05T00:00:00.000Z'),
319+
};
320+
321+
const state: any = {
322+
moduleBank: { modules: {} },
323+
timetables: {
324+
lessons: {
325+
[4]: {
326+
CS3216: {},
327+
},
328+
[1]: {
329+
CS1010S: {},
330+
},
331+
},
332+
colors: {
333+
[4]: COLORS,
334+
[1]: COLORS,
335+
},
336+
},
337+
};
338+
339+
// Should return sem1 timetable
340+
expect(mapStateToProps(state, ownProps).timetableWithLessons).toHaveProperty('CS1010S');
341+
});
342+
});
343+
344+
describe(mapStateToProps, () => {
345+
test('should use correct semester (test 4)', () => {
346+
// On week -1 of special term I, it should be sem2
347+
const ownProps: any = {
348+
currentTime: new Date('2025-05-11T00:00:00.000Z'),
349+
};
350+
351+
const state: any = {
352+
moduleBank: { modules: {} },
353+
timetables: {
354+
lessons: {
355+
[2]: {
356+
CS3216: {},
357+
},
358+
[3]: {
359+
CS1010S: {},
360+
},
361+
},
362+
colors: {
363+
[2]: COLORS,
364+
[3]: COLORS,
365+
},
366+
},
367+
};
368+
369+
// Should return sem2 timetable
370+
expect(mapStateToProps(state, ownProps).timetableWithLessons).toHaveProperty('CS3216');
371+
});
372+
});
373+
374+
describe(mapStateToProps, () => {
375+
test('should use correct semester (test 5)', () => {
376+
// On week -1 of special term II, it should be special term I
377+
const ownProps: any = {
378+
currentTime: new Date('2025-06-22T00:00:00.000Z'),
379+
};
380+
381+
const state: any = {
382+
moduleBank: { modules: {} },
383+
timetables: {
384+
lessons: {
385+
[3]: {
386+
CS3216: {},
387+
},
388+
[4]: {
389+
CS1010S: {},
390+
},
391+
},
392+
colors: {
393+
[3]: COLORS,
394+
[4]: COLORS,
395+
},
396+
},
397+
};
398+
399+
// Should return special term I timetable
400+
expect(mapStateToProps(state, ownProps).timetableWithLessons).toHaveProperty('CS3216');
401+
});
402+
});

website/src/views/today/TodayContainer/TodayContainer.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ const EMPTY_LESSONS: ColoredLesson[] = [];
5353
const semesterNameMap: Record<string, number> = {
5454
'Semester 1': 1,
5555
'Semester 2': 2,
56-
'Special Sem 1': 3,
57-
'Special Sem 2': 4,
56+
'Special Term I': 3,
57+
'Special Term II': 4,
5858
};
5959

6060
export type OwnProps = TimerData;
@@ -347,8 +347,19 @@ export class TodayContainerComponent extends React.PureComponent<Props, State> {
347347

348348
export const mapStateToProps = (state: StoreState, ownProps: OwnProps) => {
349349
const { modules } = state.moduleBank;
350-
const lastDay = addDays(ownProps.currentTime, DAYS);
351-
const weekInfo = NUSModerator.academicCalendar.getAcadWeekInfo(lastDay);
350+
351+
const lastDay = addDays(ownProps.currentTime, DAYS); // current date plus 7 days
352+
const todayWeekInfo = NUSModerator.academicCalendar.getAcadWeekInfo(ownProps.currentTime);
353+
const nextWeekInfo = NUSModerator.academicCalendar.getAcadWeekInfo(lastDay);
354+
355+
const todaySemester = semesterNameMap[todayWeekInfo.sem];
356+
const nextWeekSemester = semesterNameMap[nextWeekInfo.sem];
357+
358+
// On week -1 of semester 2, the semester should be 2, not 1
359+
const weekBeforeSem2 = todaySemester === 1 && nextWeekSemester === 2;
360+
// If it's the week before semester 2, use sem2's week info, otherwise use current date's week info
361+
const weekInfo = weekBeforeSem2 ? nextWeekInfo : todayWeekInfo;
362+
352363
const semester = semesterNameMap[weekInfo.sem];
353364
const timetable = getSemesterTimetableLessons(state)(semester);
354365
const colors = getSemesterTimetableColors(state)(semester);

0 commit comments

Comments
 (0)