From adb65b04da7e12a2b013b8c304a033f0a5e74c23 Mon Sep 17 00:00:00 2001 From: "a.bouhuolia" Date: Sat, 20 Mar 2021 22:25:36 +0200 Subject: [PATCH] fix(Redux): fix the presisted redux store. --- client/src/hooks/query/authentication.js | 4 ++++ client/src/hooks/state/authentication.js | 2 -- client/src/store/ResetMiddleware.js | 13 +++++++++++++ client/src/store/createStore.js | 16 ++++++---------- 4 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 client/src/store/ResetMiddleware.js diff --git a/client/src/hooks/query/authentication.js b/client/src/hooks/query/authentication.js index dc86ac5e8..37926b64d 100644 --- a/client/src/hooks/query/authentication.js +++ b/client/src/hooks/query/authentication.js @@ -1,6 +1,7 @@ import { useMutation } from 'react-query'; import useApiRequest from '../useRequest'; import { useAuthActions } from '../state'; +import { persistor } from 'store/createStore'; /** * Authentication login. @@ -15,6 +16,9 @@ export const useAuthLogin = (props) => { select: (res) => res.data, onSuccess: (data) => { setLogin(data.data); + + // Run the store persist. + persistor.persist(); }, ...props } diff --git a/client/src/hooks/state/authentication.js b/client/src/hooks/state/authentication.js index ac2b9558d..995195b87 100644 --- a/client/src/hooks/state/authentication.js +++ b/client/src/hooks/state/authentication.js @@ -3,10 +3,8 @@ import { useCallback } from 'react'; import { isAuthenticated } from 'store/authentication/authentication.reducer'; import { setLogin, - setLogout, setStoreReset, } from 'store/authentication/authentication.actions'; -import { purgePersistedState } from 'store/createStore'; import { useQueryClient } from 'react-query'; export const useAuthActions = () => { diff --git a/client/src/store/ResetMiddleware.js b/client/src/store/ResetMiddleware.js new file mode 100644 index 000000000..2c0ff73d5 --- /dev/null +++ b/client/src/store/ResetMiddleware.js @@ -0,0 +1,13 @@ +export default (next) => (reducer, initialState, enhancer) => { + let resetType = 'RESET' + let resetData = 'state' + + const enhanceReducer = (state, action) => { + if (action.type === resetType) { + state = action[resetData] + } + return reducer(state, action) + } + + return next(enhanceReducer, initialState, enhancer) +} \ No newline at end of file diff --git a/client/src/store/createStore.js b/client/src/store/createStore.js index 0110455e8..a91594850 100644 --- a/client/src/store/createStore.js +++ b/client/src/store/createStore.js @@ -4,17 +4,11 @@ import { compose, } from 'redux'; import thunkMiddleware from 'redux-thunk'; -import { persistStore, persistReducer, purgeStoredState } from 'redux-persist'; -import storage from 'redux-persist/lib/storage'; +import { persistStore } from 'redux-persist'; import monitorReducerEnhancer from 'store/enhancers/monitorReducer'; import loggerMiddleware from 'middleware/logger'; import rootReducer from 'store/reducers'; - -const persistConfig = { - key: 'bigcapital:root', - blacklist: ['dashboard'], - storage, -}; +import ResetMiddleware from './ResetMiddleware'; const createStoreFactory = (initialState = {}) => { /** @@ -22,14 +16,14 @@ const createStoreFactory = (initialState = {}) => { | Middleware Configuration |-------------------------------------------------- */ - const middleware = [thunkMiddleware, loggerMiddleware]; + const middleware = [thunkMiddleware, loggerMiddleware ]; /** |-------------------------------------------------- | Store Enhancers |-------------------------------------------------- */ - const enhancers = [monitorReducerEnhancer]; + const enhancers = [monitorReducerEnhancer, ResetMiddleware]; let composeEnhancers = compose; if (process.env.NODE_ENV === 'development') { @@ -47,6 +41,8 @@ const createStoreFactory = (initialState = {}) => { rootReducer, initialState, composeEnhancers(applyMiddleware(...middleware), ...enhancers), + + ); store.asyncReducers = {}; return store;