-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.ts
157 lines (138 loc) · 5.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import type { Action, Reducer, ReducersMapObject, AnyAction } from 'redux';
import type { PersistConfig } from 'redux-persist';
import { createTransform, persistCombineReducers, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // defaults to localStorage
import createCompressor from 'redux-persist-transform-compress';
import { createFilter } from 'redux-persist-transform-filter';
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import { osrdGatewayApi } from 'common/api/osrdGatewayApi';
import type { EditorSlice, EditorState } from 'reducers/editor';
import editorReducer, { editorInitialState, editorSlice } from 'reducers/editor';
import mainReducer, { mainInitialState, mainSlice } from 'reducers/main';
import type { MainState } from 'reducers/main';
import mapReducer, { mapInitialState, mapSlice } from 'reducers/map';
import type { MapState } from 'reducers/map';
import type { MapViewerState, MapViewerSlice } from 'reducers/mapViewer';
import mapViewerReducer, { mapViewerInitialState, mapViewerSlice } from 'reducers/mapViewer';
import operationalStudiesConfReducer, {
operationalStudiesConfSlice,
} from 'reducers/osrdconf/operationalStudiesConf';
import stdcmConfReducer, {
stdcmConfInitialState,
stdcmConfSlice,
} from 'reducers/osrdconf/stdcmConf';
import type { OsrdConfState, OsrdStdcmConfState } from 'reducers/osrdconf/types';
import simulationReducer, {
simulationResultsInitialState,
simulationResultsSlice,
} from 'reducers/simulationResults';
import type { SimulationResultsState } from 'reducers/simulationResults/types';
import userReducer, { userInitialState, userSlice } from 'reducers/user';
import type { UserState } from 'reducers/user';
import { type ConfSlice, defaultCommonConf } from './osrdconf/osrdConfCommon';
const compressor = createCompressor({
whitelist: ['rollingstock'],
});
const mapWhiteList = [
'mapStyle',
'showOrthoPhoto',
'showOSM',
'layers',
'layersSettings',
'userPreference',
'terrain3DExaggeration',
];
const userWhiteList = ['account', 'userPreferences'];
const mainWhiteList = ['lastInterfaceVersion'];
const saveMapFilter = createFilter('map', mapWhiteList);
const saveUserFilter = createFilter('user', userWhiteList);
const saveMainFilter = createFilter('main', mainWhiteList);
// Deserialize date strings coming from local storage
const stdcmPathStepsDateTransform = createTransform(
null,
(outboundState: { arrival?: string }[]) =>
outboundState.map(({ arrival, ...step }) => {
if (arrival) {
return { ...step, arrival: new Date(arrival) };
}
return step;
}),
{ whitelist: ['stdcmPathSteps'] }
);
const operationalStudiesDateTransform = createTransform(
null,
({ startTime, ...outboundState }: { startTime: string }) => ({
...outboundState,
startTime: new Date(startTime),
}),
{ whitelist: ['operationalStudiesConf'] }
);
// Useful to only blacklist a sub-propertie of osrdconf
const buildOsrdConfPersistConfig = <T extends OsrdConfState>(
slice: ConfSlice
): PersistConfig<T> => ({
key: slice.name,
storage,
transforms: [stdcmPathStepsDateTransform, operationalStudiesDateTransform],
});
export const persistConfig = {
key: 'root',
storage,
transforms: [compressor, saveMapFilter, saveUserFilter, saveMainFilter],
blacklist: [stdcmConfSlice.name, operationalStudiesConfSlice.name],
whitelist: ['user', 'map', 'main', 'simulation', 'mapViewer'],
};
type AllActions = Action;
export type OsrdSlice = ConfSlice | EditorSlice | MapViewerSlice;
export interface RootState {
[userSlice.name]: UserState;
[mapSlice.name]: MapState;
[mapViewerSlice.name]: MapViewerState;
[editorSlice.name]: EditorState;
[mainSlice.name]: MainState;
[stdcmConfSlice.name]: OsrdStdcmConfState;
[operationalStudiesConfSlice.name]: OsrdConfState;
[simulationResultsSlice.name]: SimulationResultsState;
[osrdEditoastApi.reducerPath]: ReturnType<typeof osrdEditoastApi.reducer>;
[osrdGatewayApi.reducerPath]: ReturnType<typeof osrdGatewayApi.reducer>;
}
export const rootInitialState: RootState = {
[userSlice.name]: userInitialState,
[mapSlice.name]: mapInitialState,
[mapViewerSlice.name]: mapViewerInitialState,
[editorSlice.name]: editorInitialState,
[mainSlice.name]: mainInitialState,
[stdcmConfSlice.name]: stdcmConfInitialState,
[operationalStudiesConfSlice.name]: defaultCommonConf,
[simulationResultsSlice.name]: simulationResultsInitialState,
[osrdEditoastApi.reducerPath]: {} as ReturnType<typeof osrdEditoastApi.reducer>,
[osrdGatewayApi.reducerPath]: {} as ReturnType<typeof osrdGatewayApi.reducer>,
};
export type AnyReducerState =
| UserState
| MapState
| MapViewerState
| EditorState
| MainState
| OsrdStdcmConfState
| OsrdConfState
| SimulationResultsState;
export const rootReducer: ReducersMapObject<RootState> = {
[userSlice.name]: userReducer,
[mapSlice.name]: mapReducer,
[mapViewerSlice.name]: mapViewerReducer,
[editorSlice.name]: editorReducer as Reducer<EditorState, AnyAction>,
[mainSlice.name]: mainReducer,
[stdcmConfSlice.name]: persistReducer(
buildOsrdConfPersistConfig<OsrdStdcmConfState>(stdcmConfSlice),
stdcmConfReducer
) as unknown as Reducer<OsrdStdcmConfState, AnyAction>,
[operationalStudiesConfSlice.name]: persistReducer(
buildOsrdConfPersistConfig<OsrdConfState>(operationalStudiesConfSlice),
operationalStudiesConfReducer
) as unknown as Reducer<OsrdConfState, AnyAction>,
[simulationResultsSlice.name]: simulationReducer,
[osrdEditoastApi.reducerPath]: osrdEditoastApi.reducer,
[osrdGatewayApi.reducerPath]: osrdGatewayApi.reducer,
};
export default persistCombineReducers<RootState, AllActions>(persistConfig, rootReducer);