@@ -6,7 +6,9 @@ import { createMemoryHistory } from 'history';
66import { compose , applyMiddleware , legacy_createStore as createStore } from 'redux' ;
77import { Provider } from 'react-redux' ;
88import promiseMiddleware from 'redux-promise' ;
9- import persistState from 'redux-localstorage' ;
9+ import { persistStore , persistReducer } from 'redux-persist' ;
10+ import storage from 'redux-persist/lib/storage' ; // localStorage
11+ import { PersistGate } from 'redux-persist/integration/react' ;
1012import 'bootstrap-sass' ;
1113
1214import queueHandler from './actions/queueHandler' ;
@@ -36,15 +38,38 @@ import isomorphicFetch from 'isomorphic-fetch';
3638isomorphicFetch ;
3739moment . locale ( 'fi' ) ;
3840
39- const stateToPersist = [ 'data' , 'auth' , 'updateQueue' , 'unitsByUpdateTime' , 'unitsByUpdateCount' , 'serviceGroup' ] ;
41+ const persistConfig = {
42+ key : 'root' ,
43+ storage,
44+ whitelist : [ 'auth' , 'updateQueue' , 'unitsByUpdateTime' , 'unitsByUpdateCount' , 'serviceGroup' ] , // Only persist these
45+ transforms : [
46+ // Transform to exclude loading from data if we include data
47+ {
48+ in : ( state , key ) => {
49+ if ( key === 'data' && state ) {
50+ // eslint-disable-next-line no-unused-vars
51+ const { loading, ...stateWithoutLoading } = state ;
52+ return stateWithoutLoading ;
53+ }
54+ return state ;
55+ } ,
56+ // eslint-disable-next-line no-unused-vars
57+ out : ( state , key ) => state
58+ }
59+ ]
60+ } ;
4061
41- const finalCreateStore = compose (
42- applyMiddleware ( promiseMiddleware ) ,
43- persistState ( stateToPersist ) ) ( createStore ) ;
62+ const persistedReducer = persistReducer ( persistConfig , rootReducer ) ;
4463
45- const enableReduxDevTools = window . __REDUX_DEVTOOLS_EXTENSION__ && window . __REDUX_DEVTOOLS_EXTENSION__ ( ) ;
64+ const store = createStore (
65+ persistedReducer ,
66+ compose (
67+ applyMiddleware ( promiseMiddleware ) ,
68+ window . __REDUX_DEVTOOLS_EXTENSION__ && window . __REDUX_DEVTOOLS_EXTENSION__ ( )
69+ )
70+ ) ;
4671
47- const store = finalCreateStore ( rootReducer , enableReduxDevTools ) ;
72+ const persistor = persistStore ( store ) ;
4873window . store = store ;
4974
5075const rootElement = document . getElementById ( 'app' ) ;
@@ -53,24 +78,26 @@ const root = createRoot(rootElement);
5378// Render the main component into the dom
5479root . render (
5580 < Provider store = { store } >
56- < BrowserRouter history = { createMemoryHistory ( ) } >
57- < Routes >
58- < Route path = "/login" element = { < LoginScreen /> } />
59- < Route path = "/" element = { < App /> } >
60- < Route exact path = "/" element = { < DashBoard /> } />
61- < Route path = "/group" element = { < GroupList /> } />
62- < Route path = "/group/:groupId" element = { < UnitList /> } />
63- < Route path = "/group/:groupId/mass-edit" element = { < UnitMassEditPropertySelect /> } />
64- < Route path = "/group/:groupId/mass-edit/:propertyId" element = { < UnitMassEdit /> } />
65- < Route path = "/unit/:unitId" element = { < UnitDetails /> } />
66- < Route path = "/unit/:unitId/history" element = { < UnitHistory /> } />
67- < Route path = "/unit/:unitId/update/:propertyId/:valueId" element = { < UpdateConfirmation /> } />
68- < Route path = "/unit/:unitId/delete/:propertyId" element = { < DeleteConfirmation /> } />
69- < Route path = "/queue" element = { < UpdateQueue /> } />
70- < Route path = "*" element = { < NotFound /> } />
71- </ Route >
72- </ Routes >
73- </ BrowserRouter >
81+ < PersistGate loading = { < div > Ladataan...</ div > } persistor = { persistor } >
82+ < BrowserRouter history = { createMemoryHistory ( ) } >
83+ < Routes >
84+ < Route path = "/login" element = { < LoginScreen /> } />
85+ < Route path = "/" element = { < App /> } >
86+ < Route exact path = "/" element = { < DashBoard /> } />
87+ < Route path = "/group" element = { < GroupList /> } />
88+ < Route path = "/group/:groupId" element = { < UnitList /> } />
89+ < Route path = "/group/:groupId/mass-edit" element = { < UnitMassEditPropertySelect /> } />
90+ < Route path = "/group/:groupId/mass-edit/:propertyId" element = { < UnitMassEdit /> } />
91+ < Route path = "/unit/:unitId" element = { < UnitDetails /> } />
92+ < Route path = "/unit/:unitId/history" element = { < UnitHistory /> } />
93+ < Route path = "/unit/:unitId/update/:propertyId/:valueId" element = { < UpdateConfirmation /> } />
94+ < Route path = "/unit/:unitId/delete/:propertyId" element = { < DeleteConfirmation /> } />
95+ < Route path = "/queue" element = { < UpdateQueue /> } />
96+ < Route path = "*" element = { < NotFound /> } />
97+ </ Route >
98+ </ Routes >
99+ </ BrowserRouter >
100+ </ PersistGate >
74101 </ Provider >
75102) ;
76103
0 commit comments