Last active
April 11, 2018 19:08
-
-
Save leggomuhgreggo/dd30662045d5e3d7b38c7b603cec6161 to your computer and use it in GitHub Desktop.
much conditional. very overlap circles venn diagram.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
const checkSessionThunk = async (dispatch, getState, actionObj) => { | |
const session = getConditions(dispatch, getState, actionObj); | |
if (session.validSessionAndRedirectDeclared) { | |
return dispatch(goToRedirectURL()); | |
} | |
if (session.validSessionAndRouteLogin) { | |
return dispatch(goToResources()); | |
} | |
if (session.invalidSessionAndRoutePrivileged) { | |
return dispatch(goToLogout()); | |
} | |
if (session.noSessionAndRoutePrivileged) { | |
return dispatch(goToLogin()); | |
} | |
}; | |
export default checkSessionThunk; | |
const getConditions = (dispatch, getState, actionObj) => { | |
const { user, location: { routesMap }, redirectURL } = getState(); | |
const { | |
action: { type: currentRouteType, payload: { originalAction } }, | |
} = actionObj; | |
return { | |
get validSessionAndRedirectDeclared() { | |
return ( | |
userHasExistingSession(user) && | |
!userSessionHasExpired(user) && | |
redirectURL && | |
redirectURL.url | |
); | |
}, | |
get validSessionAndRouteLogin() { | |
return ( | |
userHasExistingSession(user) && | |
!userSessionHasExpired(user) && | |
isRouteTypeLogin(currentRouteType) | |
); | |
}, | |
get invalidSessionAndRoutePrivileged() { | |
return ( | |
userHasExistingSession(user) && | |
userSessionHasExpired(user) && | |
isPrivilegedRoute({ routesMap, currentRouteType }) | |
); | |
}, | |
get noSessionAndRoutePrivileged() { | |
return ( | |
!userHasExistingSession(user) && | |
isPrivilegedRoute({ routesMap, currentRouteType }) | |
); | |
}, | |
get originalActionFromPayload() { | |
return ( | |
originalAction.type && originalAction.type !== checkSessionAction.type | |
); | |
}, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment