From 94da1f67226f1b1b9974426536ba2a445c5e99f2 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 24 Mar 2021 16:49:24 -0500 Subject: [PATCH] Reload the page conditionally --- app/soapbox/reducers/auth.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/soapbox/reducers/auth.js b/app/soapbox/reducers/auth.js index dc9e67f4f..1a9a72c82 100644 --- a/app/soapbox/reducers/auth.js +++ b/app/soapbox/reducers/auth.js @@ -116,13 +116,28 @@ const reducer = (state, action) => { } }; -export default function auth(oldState = initialState, action) { - const state = reducer(oldState, action); - localStorage.setItem('soapbox:auth', JSON.stringify(state.toJS())); +const maybeReload = (oldState, state, action) => { + const conds = [ + action.type === SWITCH_ACCOUNT, + action.type === VERIFY_CREDENTIALS_FAIL && state.get('me') !== oldState.get('me'), + ]; - if (action.type === SWITCH_ACCOUNT) { + // Reload if any of these conditions are true + const shouldReload = conds.some(cond => cond); + + if (shouldReload) { location.reload(); } +}; + +export default function auth(oldState = initialState, action) { + const state = reducer(oldState, action); + + // Persist the state in localStorage + localStorage.setItem('soapbox:auth', JSON.stringify(state.toJS())); + + // Reload the page under some conditions + maybeReload(oldState, state, action); return state; };