fix(Redux): fix the presisted redux store.

This commit is contained in:
a.bouhuolia
2021-03-20 22:25:36 +02:00
parent cd2e93743e
commit adb65b04da
4 changed files with 23 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
import { useMutation } from 'react-query'; import { useMutation } from 'react-query';
import useApiRequest from '../useRequest'; import useApiRequest from '../useRequest';
import { useAuthActions } from '../state'; import { useAuthActions } from '../state';
import { persistor } from 'store/createStore';
/** /**
* Authentication login. * Authentication login.
@@ -15,6 +16,9 @@ export const useAuthLogin = (props) => {
select: (res) => res.data, select: (res) => res.data,
onSuccess: (data) => { onSuccess: (data) => {
setLogin(data.data); setLogin(data.data);
// Run the store persist.
persistor.persist();
}, },
...props ...props
} }

View File

@@ -3,10 +3,8 @@ import { useCallback } from 'react';
import { isAuthenticated } from 'store/authentication/authentication.reducer'; import { isAuthenticated } from 'store/authentication/authentication.reducer';
import { import {
setLogin, setLogin,
setLogout,
setStoreReset, setStoreReset,
} from 'store/authentication/authentication.actions'; } from 'store/authentication/authentication.actions';
import { purgePersistedState } from 'store/createStore';
import { useQueryClient } from 'react-query'; import { useQueryClient } from 'react-query';
export const useAuthActions = () => { export const useAuthActions = () => {

View File

@@ -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)
}

View File

@@ -4,17 +4,11 @@ import {
compose, compose,
} from 'redux'; } from 'redux';
import thunkMiddleware from 'redux-thunk'; import thunkMiddleware from 'redux-thunk';
import { persistStore, persistReducer, purgeStoredState } from 'redux-persist'; import { persistStore } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import monitorReducerEnhancer from 'store/enhancers/monitorReducer'; import monitorReducerEnhancer from 'store/enhancers/monitorReducer';
import loggerMiddleware from 'middleware/logger'; import loggerMiddleware from 'middleware/logger';
import rootReducer from 'store/reducers'; import rootReducer from 'store/reducers';
import ResetMiddleware from './ResetMiddleware';
const persistConfig = {
key: 'bigcapital:root',
blacklist: ['dashboard'],
storage,
};
const createStoreFactory = (initialState = {}) => { const createStoreFactory = (initialState = {}) => {
/** /**
@@ -22,14 +16,14 @@ const createStoreFactory = (initialState = {}) => {
| Middleware Configuration | Middleware Configuration
|-------------------------------------------------- |--------------------------------------------------
*/ */
const middleware = [thunkMiddleware, loggerMiddleware]; const middleware = [thunkMiddleware, loggerMiddleware ];
/** /**
|-------------------------------------------------- |--------------------------------------------------
| Store Enhancers | Store Enhancers
|-------------------------------------------------- |--------------------------------------------------
*/ */
const enhancers = [monitorReducerEnhancer]; const enhancers = [monitorReducerEnhancer, ResetMiddleware];
let composeEnhancers = compose; let composeEnhancers = compose;
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
@@ -47,6 +41,8 @@ const createStoreFactory = (initialState = {}) => {
rootReducer, rootReducer,
initialState, initialState,
composeEnhancers(applyMiddleware(...middleware), ...enhancers), composeEnhancers(applyMiddleware(...middleware), ...enhancers),
); );
store.asyncReducers = {}; store.asyncReducers = {};
return store; return store;