-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersist ts
45 lines (39 loc) · 1.28 KB
/
Persist 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
import { configureStore, Middleware } from "@reduxjs/toolkit";
import tollGateReducer from "./components/addTollGate/store/tollGateSlice";
import tollEntryReducer from "./components/addTollEntries/store/tollEntrySlice";
interface ApplicationState {
// Define the state shape of your reducers here
tollEntry: TollEntryState;
tollGate: TollGateState;
// Add more reducers if needed
}
interface TollEntryState {
// Define the state shape for tollEntryReducer here
// Example: property1: string;
}
interface TollGateState {
// Define the state shape for tollGateReducer here
// Example: property2: number;
}
const localStorageMiddleware: Middleware = ({ getState }) => {
return (next) => (action) => {
const result = next(action);
localStorage.setItem('applicationState', JSON.stringify(getState()));
return result;
};
};
const reHydrateStore = () => {
const storedState = localStorage.getItem('applicationState');
if (storedState !== null) {
return JSON.parse(storedState) as ApplicationState;
}
};
export default configureStore({
reducer: {
tollEntry: tollEntryReducer,
tollGate: tollGateReducer,
},
preloadedState: reHydrateStore(),
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware<ApplicationState>().concat(localStorageMiddleware),
});