1
1
import { get , omit , values } from 'lodash' ;
2
2
import produce from 'immer' ;
3
- import { createMigrate } from 'redux-persist' ;
3
+ import { createMigrate , PersistedState } from 'redux-persist' ;
4
4
5
5
import { PersistConfig } from 'storage/persistReducer' ;
6
6
import { ModuleCode } from 'types/modules' ;
7
- import { ModuleLessonConfig , SemTimetableConfig } from 'types/timetables' ;
7
+ import { ModuleLessonConfig , SemTimetableConfig , TimetableConfig } from 'types/timetables' ;
8
8
import { ColorMapping , TimetablesState } from 'types/reducers' ;
9
9
10
10
import config from 'config' ;
@@ -22,6 +22,40 @@ import { getNewColor } from 'utils/colors';
22
22
import { SET_EXPORTED_DATA } from 'actions/constants' ;
23
23
import { Actions } from '../types/actions' ;
24
24
25
+ // Migration from state V1 -> V2
26
+ type TimetableStateV1 = Omit < TimetablesState , 'lessons' > & {
27
+ lessons : { [ semester : string ] : { [ moduleCode : string ] : { [ lessonType : string ] : string } } } ;
28
+ } ;
29
+ export function migrateV1toV2 (
30
+ oldState : TimetableStateV1 & PersistedState ,
31
+ ) : TimetablesState & PersistedState {
32
+ const newLessons : TimetableConfig = { } ;
33
+ const oldLessons = oldState . lessons ;
34
+
35
+ Object . entries ( oldLessons ) . forEach ( ( [ semester , modules ] ) => {
36
+ Object . entries ( modules ) . forEach ( ( [ moduleCode , lessons ] ) => {
37
+ const newSemester : { [ moduleCode : string ] : { [ lessonType : string ] : string [ ] } } = {
38
+ [ moduleCode ] : { } ,
39
+ } ;
40
+
41
+ Object . entries ( lessons ) . forEach ( ( [ lessonType , lessonValue ] ) => {
42
+ const lessonArray = [ lessonValue ] ;
43
+ newSemester [ moduleCode ] [ lessonType ] = lessonArray ;
44
+ } ) ;
45
+
46
+ if ( ! newLessons [ semester ] ) {
47
+ newLessons [ semester ] = { } ;
48
+ }
49
+ Object . assign ( newLessons [ semester ] , newSemester ) ;
50
+ } ) ;
51
+ } ) ;
52
+
53
+ return {
54
+ ...oldState ,
55
+ lessons : newLessons ,
56
+ } ;
57
+ }
58
+
25
59
export const persistConfig = {
26
60
/* eslint-disable no-useless-computed-key */
27
61
migrate : createMigrate ( {
@@ -34,9 +68,12 @@ export const persistConfig = {
34
68
// eslint-disable-next-line no-underscore-dangle, @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain
35
69
_persist : state ?. _persist ! ,
36
70
} ) ,
71
+ // Same as planner.ts
72
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
+ [ 2 ] : migrateV1toV2 as any ,
37
74
} ) ,
38
75
/* eslint-enable */
39
- version : 1 ,
76
+ version : 2 ,
40
77
41
78
// Our own state reconciler archives old timetables if the acad year is different,
42
79
// otherwise use the persisted timetable state
@@ -82,7 +119,7 @@ function moduleLessonConfig(
82
119
if ( ! ( classNo && lessonType ) ) return state ;
83
120
return {
84
121
...state ,
85
- [ lessonType ] : classNo ,
122
+ [ lessonType ] : [ classNo ] ,
86
123
} ;
87
124
}
88
125
case SET_LESSON_CONFIG :
0 commit comments