Skip to content

Commit

Permalink
timetables: add customisedModules prop when migrating to V2 schema
Browse files Browse the repository at this point in the history
  • Loading branch information
zwliew committed Aug 4, 2023
1 parent 4ce23c1 commit eeeb60f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions website/src/reducers/timetables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ describe('redux schema migration', () => {
hidden: {},
academicYear: '2022/2023',
archive: {},
customisedModules: {},
_persist: {
version: 1,
rehydrated: false,
Expand All @@ -285,7 +284,10 @@ describe('redux schema migration', () => {
hidden: {},
academicYear: '2022/2023',
archive: {},
customisedModules: {},
customisedModules: {
[1]: [],
[2]: [],
},
_persist: {
version: 1, // version kept the same because the framework does not support it in unit tests
rehydrated: false,
Expand Down
8 changes: 6 additions & 2 deletions website/src/reducers/timetables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createMigrate, PersistedState } from 'redux-persist';
import { PersistConfig } from 'storage/persistReducer';
import { ModuleCode } from 'types/modules';
import { ModuleLessonConfig, SemTimetableConfig, TimetableConfig } from 'types/timetables';
import { ColorMapping, TimetablesState } from 'types/reducers';
import { ColorMapping, CustomisedModulesMap, TimetablesState } from 'types/reducers';

import config from 'config';
import {
Expand All @@ -27,16 +27,19 @@ import { SET_EXPORTED_DATA } from 'actions/constants';
import { Actions } from '../types/actions';

// Migration from state V1 -> V2
type TimetableStateV1 = Omit<TimetablesState, 'lessons'> & {
type TimetableStateV1 = Omit<TimetablesState, 'lessons' | 'customisedModules'> & {
lessons: { [semester: string]: { [moduleCode: string]: { [lessonType: string]: string } } };
};
export function migrateV1toV2(
oldState: TimetableStateV1 & PersistedState,
): TimetablesState & PersistedState {
const newLessons: TimetableConfig = {};
const oldLessons = oldState.lessons;
const newCustomisedModules: CustomisedModulesMap = {};

Object.entries(oldLessons).forEach(([semester, modules]) => {
newCustomisedModules[semester] = [];

Object.entries(modules).forEach(([moduleCode, lessons]) => {
const newSemester: { [moduleCode: string]: { [lessonType: string]: string[] } } = {
[moduleCode]: {},
Expand All @@ -56,6 +59,7 @@ export function migrateV1toV2(
return {
...oldState,
lessons: newLessons,
customisedModules: newCustomisedModules,
};
}

Expand Down

0 comments on commit eeeb60f

Please sign in to comment.