From 8e84c83fdc47a999b5789dedc0b2ca95776daf59 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 29 Mar 2021 20:03:27 -0500 Subject: [PATCH] Refactor persistState() --- app/soapbox/reducers/auth.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/app/soapbox/reducers/auth.js b/app/soapbox/reducers/auth.js index 7f0938ac3..51b1be464 100644 --- a/app/soapbox/reducers/auth.js +++ b/app/soapbox/reducers/auth.js @@ -16,6 +16,7 @@ const defaultState = ImmutableMap({ me: null, }); +const sessionUser = sessionStorage.getItem('soapbox:auth:me'); const localState = fromJS(JSON.parse(localStorage.getItem('soapbox:auth'))); // If `me` doesn't match an existing user, attempt to shift it. @@ -30,15 +31,7 @@ const maybeShiftMe = state => { } }; -const setSessionUser = state => { - const sessionUser = sessionStorage.getItem('soapbox:auth:me'); - if (sessionUser) { - return state.set('me', sessionUser); - } else { - sessionStorage.setItem('soapbox:auth:me', state.get('me', null)); - return state; - } -}; +const setSessionUser = state => state.update('me', null, me => sessionUser || me); // Upgrade the initial state const migrateLegacy = state => { @@ -61,15 +54,21 @@ const migrateLegacy = state => { }); }; +const persistState = state => { + localStorage.setItem('soapbox:auth', JSON.stringify(state.toJS())); + sessionStorage.setItem('soapbox:auth:me', state.get('me')); +}; + const initialize = state => { return state.withMutations(state => { maybeShiftMe(state); setSessionUser(state); migrateLegacy(state); + persistState(state); }); }; -const initialState = defaultState.merge(localState).withMutations(initialize); +const initialState = initialize(defaultState.merge(localState)); const importToken = (state, token) => { return state.setIn(['tokens', token.access_token], fromJS(token)); @@ -162,8 +161,7 @@ export default function auth(oldState = initialState, action) { // Persist the state in localStorage if (!state.equals(oldState)) { - localStorage.setItem('soapbox:auth', JSON.stringify(state.toJS())); - sessionStorage.setItem('soapbox:auth:me', state.get('me')); + persistState(state); // Reload the page under some conditions maybeReload(oldState, state, action);