diff --git a/js/components/ascribe_routes/proxy_routes/redirect_proxy_handler.js b/js/components/ascribe_routes/proxy_routes/redirect_proxy_handler.js index 474d07fd..4fae7da2 100644 --- a/js/components/ascribe_routes/proxy_routes/redirect_proxy_handler.js +++ b/js/components/ascribe_routes/proxy_routes/redirect_proxy_handler.js @@ -11,6 +11,14 @@ import AppConstants from '../../../constants/application_constants'; const { object } = React.PropTypes; +/** + * Can be used in combination with `ProxyRoute` as an intermediate Handler + * between the actual component we want to display dependent on a certain state + * that is required to display that component. + * + * @param {string} options.to Any type of route path that is defined in routes.js + * @param {enum/string} options.when ('loggedIn' || 'loggedOut') + */ export default function RedirectProxyHandler({to, when}) { return (Component) => { return React.createClass({ @@ -41,19 +49,32 @@ export default function RedirectProxyHandler({to, when}) { const { query } = this.props.location; const { redirectAuthenticated, redirect } = query; + // validate when as an enum, that is either 'loggedIn' or 'loggedOut'. + // Otherwise throw an error. if(when === 'loggedIn' || when === 'loggedOut') { + // The user of this Handler specifies with `when`, what kind of status + // needs to be checked to conditionally do - if that state is `true` - + // a redirect. + // + // So if when === 'loggedIn', we're checking if the user is logged in and + // vice versa. let exprToValidate = when === 'loggedIn' ? this.state.currentUser && this.state.currentUser.email : this.state.currentUser && !this.state.currentUser.email; + // and redirect if `true`. if(exprToValidate) { window.setTimeout(() => this.history.pushState(null, to, query)); + + // Otherwise there can also be the case that the backend + // wants to redirect the user to a specific case when he's logged out already } else if(!exprToValidate && when === 'loggedIn' && redirect) { delete query.redirect; window.setTimeout(() => this.history.pushState(null, '/' + redirect, query)); + // and when he's logged in already } else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) { /* * redirectAuthenticated contains an arbirary path