From 9da2d8a559d92dda9c0413568aaf774e2ae98b3f Mon Sep 17 00:00:00 2001 From: ortonomy Date: Tue, 20 Feb 2018 15:20:21 +0800 Subject: [PATCH] issue #21 - adds auth HoCs to for redirecting with or without an organization - [x] adds hasOrgRedirect to go to /main if on /enroll and has an org already - [x] adds withOrg to send back to /enroll if on /main and you don't have an org yet - [x] wraps /enroll and /main components in protection - [x] updates redux hydration to make sure correct data is sent back and not null errors occur --- src/components/Main/Main.js | 3 +- src/containers/Auth/hasOrgRedirect/index.js | 37 +++++++++++++++++++++ src/containers/Auth/withOrg/index.js | 37 +++++++++++++++++++++ src/containers/OrgEnrolment/component.js | 3 +- src/utils/Api/index.js | 3 +- src/utils/Redux/index.js | 7 ++-- 6 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 src/containers/Auth/hasOrgRedirect/index.js create mode 100644 src/containers/Auth/withOrg/index.js diff --git a/src/components/Main/Main.js b/src/components/Main/Main.js index 9c4cb3e..6ce2a86 100644 --- a/src/components/Main/Main.js +++ b/src/components/Main/Main.js @@ -7,6 +7,7 @@ import { Switch, Route } from 'react-router-dom'; // component dependencies import withLogin from '../../containers/Auth/withLogin'; import withActivation from '../../containers/Auth/withActivation'; +import withOrg from '../../containers/Auth/withOrg'; import Freelancer from '../Freelancer/Freelancer'; import Project from '../Project/Project'; import NavBar from '../NavBar/NavBar'; @@ -24,4 +25,4 @@ const Main = () => ( ) -export default withLogin(withActivation(Main)); +export default withLogin(withActivation(withOrg(Main))); diff --git a/src/containers/Auth/hasOrgRedirect/index.js b/src/containers/Auth/hasOrgRedirect/index.js new file mode 100644 index 0000000..7fa3964 --- /dev/null +++ b/src/containers/Auth/hasOrgRedirect/index.js @@ -0,0 +1,37 @@ +// react +import React, { Component } from 'react'; + +// library dependencies +import { Redirect } from 'react-router-dom'; +import { connect } from 'react-redux'; + +//debug +import Debug from '../../../utils/Debug'; + + +const hasOrgRedirect = (WrappedComponent) => { + + @connect( + state => ( + { + Login: state.Login + } + ), + null + ) + class hasOrgRedirector extends Component { + render() { + Debug.log('[render] of : ', this.props); + const { Login } = this.props; + if ( Login.user.userOrg ) { + return (); + } else { + return (); + } + } + } + + return hasOrgRedirector; +} + +export default hasOrgRedirect; \ No newline at end of file diff --git a/src/containers/Auth/withOrg/index.js b/src/containers/Auth/withOrg/index.js new file mode 100644 index 0000000..b352561 --- /dev/null +++ b/src/containers/Auth/withOrg/index.js @@ -0,0 +1,37 @@ +// react +import React, { Component } from 'react'; + +// library dependencies +import { Redirect } from 'react-router-dom'; +import { connect } from 'react-redux'; + +//debug +import Debug from '../../../utils/Debug'; + + +const withOrg = (ProtectedComponent) => { + + @connect( + state => ( + { + Login: state.Login, + } + ), + null + ) + class HasOrg extends Component { + render() { + Debug.log('[render] of : ', this.props); + const { Login } = this.props; + if ( Login.user.userOrg ) { + return (); + } else { + return (); + } + } + } + + return HasOrg; +} + +export default withOrg; \ No newline at end of file diff --git a/src/containers/OrgEnrolment/component.js b/src/containers/OrgEnrolment/component.js index dfc15e2..98d4d3c 100644 --- a/src/containers/OrgEnrolment/component.js +++ b/src/containers/OrgEnrolment/component.js @@ -9,6 +9,7 @@ import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; // component dependencies +import hasOrgRedirect from '../Auth/hasOrgRedirect'; import EnrollmentForms from '../../components/EnrollmentForms'; import FormText from '../../components/FormText'; import Loader from '../../components/Loader'; @@ -157,4 +158,4 @@ class OrgEnrolment extends Component { } } -export default withRouter(OrgEnrolment); \ No newline at end of file +export default hasOrgRedirect(withRouter(OrgEnrolment)); \ No newline at end of file diff --git a/src/utils/Api/index.js b/src/utils/Api/index.js index efd374a..86d9d6e 100644 --- a/src/utils/Api/index.js +++ b/src/utils/Api/index.js @@ -7,8 +7,7 @@ import Debug from '../Debug'; class API { static async isLoggedIn(jwt) { - const query = this.generateThisUserQuery(); - return await this.Axios('POST', '/graphql', query, jwt) + return await this.Axios('POST', '/graphql', this.generateThisUserQuery(), jwt) .then ( response => ( response.thisUser ) ) .catch ( err => { Debug.log(err); diff --git a/src/utils/Redux/index.js b/src/utils/Redux/index.js index 5db9f6d..b71bee5 100644 --- a/src/utils/Redux/index.js +++ b/src/utils/Redux/index.js @@ -77,7 +77,7 @@ const hydrateState = async () => { const userDetails = user.userDetails === null ? null : user.userDetails.userAccId !== null ? user.userDetails : null; // this helps check to see if expired or invalid JWT is being used const org = userDetails && !userDetails.userOrg ? await checkOrg(userDetails.userAccId) : null; Debug.log('API.hydrateState() result: ', userDetails !== null ? userDetails : 'NOT LOGGGED IN'); - let partialState = {}; + let partialState = { Login: null, Org: null }; if ( !userDetails ) { @@ -118,10 +118,7 @@ export const configureStore = async () => { }, Org: { ...initialState.Org, - enrolment: { - ...initialState.Org.enrolment, - ...hydratedState.Org.enrolment - } + ...hydratedState.Org } } const logicMiddleware = createLogicMiddleware(rootLogic, deps); // create logic middleware