From 11dc279fd1a00ec33f83b52727f6a36a121c0964 Mon Sep 17 00:00:00 2001 From: Andreas Gasser Date: Mon, 1 Apr 2019 23:29:25 +0200 Subject: [PATCH] fix(#90): redux store is reset on logout --- src/redux/application/index.js | 5 +++++ src/redux/auth/__tests__/index.test.js | 1 + src/redux/auth/index.js | 5 ++++- src/redux/rootReducer.js | 15 +++++++++++++-- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/redux/application/index.js b/src/redux/application/index.js index 73c9a34..6a1485c 100644 --- a/src/redux/application/index.js +++ b/src/redux/application/index.js @@ -21,6 +21,7 @@ export const AppStatus = { // action types export const APP_IDLE = 'APP_IDLE'; +export const APP_RESET = 'APP_RESET'; const APPLICATION_STATUS_SET = 'APPLICATION_STATUS_SET'; @@ -29,6 +30,10 @@ export const appIdle = () => ({ type: APP_IDLE, }); +export const appReset = () => ({ + type: APP_RESET, +}); + const applicationWillLoad = () => ({ type: APPLICATION_STATUS_SET, payload: AppStatus.APPLICATION_WILL_LOAD, diff --git a/src/redux/auth/__tests__/index.test.js b/src/redux/auth/__tests__/index.test.js index a3ffc24..10e5c44 100644 --- a/src/redux/auth/__tests__/index.test.js +++ b/src/redux/auth/__tests__/index.test.js @@ -179,6 +179,7 @@ describe('auth: complex action test suite', () => { const expectedActions = [ __testables__.authLogOut(message), + reduxApplication.appReset(), ]; // set localStorage and sessionStorage diff --git a/src/redux/auth/index.js b/src/redux/auth/index.js index 4e52277..951db16 100644 --- a/src/redux/auth/index.js +++ b/src/redux/auth/index.js @@ -9,7 +9,7 @@ import hocReducer, { hocAsyncAction, hocCreateTypes } from '../HOC'; import { authRememberSelector, authUsernameSelector } from './selectors'; -import { loadApplicationAuthenticated } from '../application'; +import { loadApplicationAuthenticated, appReset } from '../application'; // action def export const AUTH_LOG_IN = 'AUTH_LOG_IN'; @@ -101,6 +101,9 @@ export const logOutUser = (message, broadcast = true) => (dispatch) => { // fire logout action dispatch(authLogOut(message)); + // clear state + dispatch(appReset()); + // clean up state try { console.log('clear local & session storage'); diff --git a/src/redux/rootReducer.js b/src/redux/rootReducer.js index ca12b82..ba77ec7 100644 --- a/src/redux/rootReducer.js +++ b/src/redux/rootReducer.js @@ -1,7 +1,7 @@ /* global requestAnimationFrame */ import { combineReducers } from 'redux'; -import applicationReducer from './application'; +import applicationReducer, { APP_RESET } from './application'; import authReducer from './auth'; import imagesReducer from './images'; import labelsReducer from './labels'; @@ -18,4 +18,15 @@ const reducers = combineReducers({ user: userReducer, }); -export default reducers; +const rootReducer = (state, action) => { + let usedState = state; + + // handle reset + if (action.type === APP_RESET) { + usedState = undefined; + } + + return reducers(usedState, action); +} + +export default rootReducer;