Created
March 10, 2018 06:33
-
-
Save iiison/eb6089749793e4d106acf48b74ace35f to your computer and use it in GitHub Desktop.
react routes
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
import React, { Component } from 'react' | |
import { Route, Switch, Redirect } from 'react-router-dom' | |
import { getCookie } from '$UTILS/cookies' | |
import { getUserDetails } from '$REDUX/modules/user' | |
import { | |
ConnectedLoginContainer, | |
ConnectedNavbarContainer, | |
ConnectedDashboardContainer, | |
ConnectedSelectStoreContainer, | |
ConnectedInvoicesContainer } from '$CONTAINERS' | |
class PrivateRoute extends Component { | |
constructor(props) { | |
super(props) | |
const { user } = props.store.getState() | |
this.state = { | |
isFetching : user.isFetching | |
} | |
} | |
render() { | |
const { component : RouteComponent, checkAuth, ...rest } = this.props | |
const route = ( | |
<Route | |
{...rest} | |
render={(props) => { | |
return checkAuth() === true | |
? <RouteComponent {...props} /> | |
: <Redirect to={{pathname : '/login'}} /> | |
}} | |
/> | |
) | |
return route | |
} | |
componentWillMount() { | |
const { store } = this.props | |
const { user } = store.getState() | |
const accessToken = getCookie('access_token') | |
if (!user.userInfo && accessToken) { | |
store.dispatch(getUserDetails(accessToken)) | |
} | |
} | |
} | |
const routes = (checkAuth, store) => ( | |
<div className='app grid'> | |
<ConnectedNavbarContainer /> | |
<div className='col'> | |
<Switch> | |
<Redirect exact={true} from='/' to='/dashboard' /> | |
<Route path='/login' component={ConnectedLoginContainer} /> | |
<PrivateRoute | |
store={store} | |
checkAuth={checkAuth} | |
path='/:id/dashboard' | |
component={ConnectedDashboardContainer} | |
/> | |
<PrivateRoute | |
store={store} | |
checkAuth={checkAuth} | |
path='/select-store' | |
component={ConnectedSelectStoreContainer} | |
/> | |
<PrivateRoute | |
store={store} | |
checkAuth={checkAuth} | |
path='/:id/finance/invoices' | |
component={ConnectedInvoicesContainer} | |
/> | |
</Switch> | |
</div> | |
</div> | |
) | |
export default routes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment