diff --git a/src/ducks/Auth.duck.js b/src/ducks/Auth.duck.js index 95787125..b6df11e7 100644 --- a/src/ducks/Auth.duck.js +++ b/src/ducks/Auth.duck.js @@ -20,6 +20,10 @@ export const SIGNUP_REQUEST = 'app/Auth/SIGNUP_REQUEST'; export const SIGNUP_SUCCESS = 'app/Auth/SIGNUP_SUCCESS'; export const SIGNUP_ERROR = 'app/Auth/SIGNUP_ERROR'; +// Generic user_logout action that can be handled elsewhere +// E.g. src/reducers.js clears store as a consequence +export const USER_LOGOUT = 'app/USER_LOGOUT'; + // ================ Reducer ================ // const initialState = { @@ -110,6 +114,8 @@ export const signupRequest = () => ({ type: SIGNUP_REQUEST }); export const signupSuccess = () => ({ type: SIGNUP_SUCCESS }); export const signupError = error => ({ type: SIGNUP_ERROR, payload: error, error: true }); +export const userLogout = () => ({ type: USER_LOGOUT }); + // ================ Thunks ================ // export const authInfo = () => @@ -150,6 +156,7 @@ export const logout = () => .logout() .then(() => dispatch(clearCurrentUser())) .then(() => dispatch(logoutSuccess())) + .then(() => dispatch(userLogout())) .catch(e => dispatch(logoutError(e))); }; diff --git a/src/ducks/Auth.test.js b/src/ducks/Auth.test.js index 36bee79b..9fe8dae1 100644 --- a/src/ducks/Auth.test.js +++ b/src/ducks/Auth.test.js @@ -15,6 +15,7 @@ import reducer, { signupRequest, signupSuccess, signupError, + userLogout, } from './Auth.duck'; // Create a dispatch function that correctly calls the thunk functions @@ -276,6 +277,7 @@ describe('Auth duck', () => { [logoutRequest()], [clearCurrentUser()], [logoutSuccess()], + [userLogout()], ]); }); }); diff --git a/src/reducers.js b/src/reducers.js index 6fa17e41..35959fa2 100644 --- a/src/reducers.js +++ b/src/reducers.js @@ -1,4 +1,5 @@ import { combineReducers } from 'redux'; +import { USER_LOGOUT } from './ducks/Auth.duck'; import * as globalReducers from './ducks'; import * as pageReducers from './containers/reducers'; @@ -9,8 +10,13 @@ import * as pageReducers from './containers/reducers'; * which is page specific. * Future: this structure could take in asyncReducers, which are changed when you navigate pages. */ -const createReducer = function createReducer() { - return combineReducers({ ...globalReducers, ...pageReducers }); +const appReducer = combineReducers({ ...globalReducers, ...pageReducers }); + +const createReducer = () => { + return (state, action) => { + const appState = action.type === USER_LOGOUT ? undefined : state; + return appReducer(appState, action); + }; }; export default createReducer;