diff --git a/src/state/actions/auth.js b/src/state/actions/auth.js index 6fceeba3..98b513f6 100644 --- a/src/state/actions/auth.js +++ b/src/state/actions/auth.js @@ -3,7 +3,7 @@ import { toastr } from 'react-redux-toastr'; import { firebaseError, FIREBASE_RESPONSE } from 'utils'; import firebase from 'firebase.js'; -import { clearUsersData } from './users'; +import { clearUsersDataLogout } from './users'; export const AUTH_SIGN_IN_INIT = createAction('AUTH_SIGN_IN_INIT'); export const AUTH_SIGN_IN_FAIL = createAction('AUTH_SIGN_IN_FAIL'); @@ -65,7 +65,7 @@ export const logout = () => { return async dispatch => { dispatch(AUTH_LOGOUT_INIT()); - dispatch(clearUsersData()); + dispatch(clearUsersDataLogout()); await firebase.auth().signOut(); dispatch(AUTH_LOGOUT_SUCCESS()); diff --git a/src/state/actions/users.js b/src/state/actions/users.js index f6777b61..7c4c4f4b 100644 --- a/src/state/actions/users.js +++ b/src/state/actions/users.js @@ -34,6 +34,8 @@ export const USERS_MODIFY_USER_FAIL = createAction('USERS_MODIFY_USER_FAIL'); export const USERS_CLEAN_UP = createAction('USERS_CLEAN_UP'); +export const USERS_CLEAR_DATA_LOGOUT = createAction('USERS_CLEAR_DATA_LOGOUT'); + export const fetchUsers = () => { return async (dispatch, getState) => { dispatch(checkUserData()); @@ -121,6 +123,12 @@ export const clearUsersData = () => { }; }; +export const clearUsersDataLogout = () => { + return dispatch => { + dispatch(USERS_CLEAR_DATA_LOGOUT()); + }; +}; + const uploadLogo = (uid, file) => { const storageRef = firebase.storage().ref(); diff --git a/src/state/reducers/users/index.js b/src/state/reducers/users/index.js index 71f6c630..39197a3b 100644 --- a/src/state/reducers/users/index.js +++ b/src/state/reducers/users/index.js @@ -14,7 +14,8 @@ import { USERS_MODIFY_USER_INIT, USERS_MODIFY_USER_SUCCESS, USERS_MODIFY_USER_FAIL, - USERS_CLEAN_UP + USERS_CLEAN_UP, + USERS_CLEAR_DATA_LOGOUT } from 'state/actions/users'; const initialState = { @@ -112,6 +113,9 @@ export const usersReducer = createReducer( error: null, success: false, deleted: false + }), + [USERS_CLEAR_DATA_LOGOUT]: () => ({ + ...initialState }) }, initialState diff --git a/src/state/reducers/users/users.test.js b/src/state/reducers/users/users.test.js index 5cd260ad..0956ce55 100644 --- a/src/state/reducers/users/users.test.js +++ b/src/state/reducers/users/users.test.js @@ -12,7 +12,8 @@ import { USERS_MODIFY_USER_INIT, USERS_MODIFY_USER_SUCCESS, USERS_MODIFY_USER_FAIL, - USERS_CLEAN_UP + USERS_CLEAN_UP, + USERS_CLEAR_DATA_LOGOUT } from 'state/actions/users'; import { usersReducer } from '.'; @@ -90,8 +91,30 @@ describe('Establishments reducer', () => { }); }); - it('should reset the state to the initial state when USERS_CLEAR_DATA action is fired', () => { - reducerTest(initialState, USERS_CLEAR_DATA(), initialState); + it('should reset the state to the initial state when USERS_CLEAR_DATA_LOGOUT action is fired', () => { + reducerTest(initialState, USERS_CLEAR_DATA_LOGOUT(), initialState); + }); + + it('should reset the state to the initial state while maintaining the data when USERS_CLEAR_DATA action is fired', () => { + const userData = [ + { + name: 'Test name', + email: 'Test email', + location: 'Test location', + createdAt: '11/20/2020' + } + ]; + + reducerTest( + { + ...initialState, + data: userData, + loading: true, + error: null + }, + USERS_CLEAR_DATA(), + { ...initialState, data: userData } + ); }); it('should set loading to true to the current state when USERS_CREATE_USER_INIT action is fired', () => {