Skip to content

Commit 4f0ead5

Browse files
committed
feat: replace redux-localstorage with redux-persist
- Update store configuration to use redux-persist instead of redux-localstorage - Add PersistGate wrapper component for state rehydration - Configure whitelist-based persistence for better control over persisted state
1 parent 298be82 commit 4f0ead5

4 files changed

Lines changed: 3358 additions & 3390 deletions

File tree

package-lock.json

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"react-router-dom": "^6.23.1",
4343
"redux": "^5.0.1",
4444
"redux-actions": "^3.0.0",
45-
"redux-localstorage": "^0.4.1",
45+
"redux-persist": "^6.0.0",
4646
"redux-promise": "^0.6.0",
4747
"sass": "^1.77.4",
4848
"sass-loader": "^14.2.1",

src/index.js

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { createMemoryHistory } from 'history';
66
import { compose, applyMiddleware, legacy_createStore as createStore } from 'redux';
77
import { Provider } from 'react-redux';
88
import 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';
1012
import 'bootstrap-sass';
1113

1214
import queueHandler from './actions/queueHandler';
@@ -36,15 +38,38 @@ import isomorphicFetch from 'isomorphic-fetch';
3638
isomorphicFetch;
3739
moment.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);
4873
window.store = store;
4974

5075
const rootElement = document.getElementById('app');
@@ -53,24 +78,26 @@ const root = createRoot(rootElement);
5378
// Render the main component into the dom
5479
root.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

Comments
 (0)