-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersist.js
30 lines (24 loc) · 844 Bytes
/
Persist.js
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
import { configureStore } from "@reduxjs/toolkit";
import tollGateReducer from "./components/addTollGate/store/tollGateSlice";
import tollEntryReducer from "./components/addTollEntries/store/tollEntrySlice";
const localStorageMiddleware = ({ getState }) => {
return next => action => {
const result = next(action);
localStorage.setItem('applicationState', JSON.stringify(getState()));
return result;
};
};
const reHydrateStore = () => {
if (localStorage.getItem('applicationState') !== null) {
return JSON.parse(localStorage.getItem('applicationState'));
}
};
export default configureStore({
reducer: {
tollEntry: tollEntryReducer,
tollGate: tollGateReducer
},
preloadedState: reHydrateStore(),
middleware: getDefaultMiddleware =>
getDefaultMiddleware().concat(localStorageMiddleware),
});