Skip to content

Instantly share code, notes, and snippets.

@leggomuhgreggo
Last active April 11, 2018 19:08
Show Gist options
  • Save leggomuhgreggo/dd30662045d5e3d7b38c7b603cec6161 to your computer and use it in GitHub Desktop.
Save leggomuhgreggo/dd30662045d5e3d7b38c7b603cec6161 to your computer and use it in GitHub Desktop.
much conditional. very overlap circles venn diagram.
...
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