Initial commit

This commit is contained in:
John Lyon-Smith
2018-02-22 17:57:27 -08:00
commit e80f5490d5
196 changed files with 38982 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import React from 'react'
import { Route, Redirect } from 'react-router-dom'
import { PropTypes } from 'prop-types'
import { api } from '../helpers'
export class ProtectedRoute extends React.Component {
static propTypes = {
roles: PropTypes.array,
location: PropTypes.shape({
pathname: PropTypes.string,
search: PropTypes.string
})
}
constructor(props) {
super(props)
this.updateComponent = this.updateComponent.bind(this)
}
updateComponent() {
this.forceUpdate()
}
componentDidMount() {
api.addListener('login', this.updateComponent)
}
componentWillUnmount() {
api.removeListener('login', this.updateComponent)
}
render(props) {
let user = api.loggedInUser
if (user) {
if (user.pending) {
// The Api might be in the middle of fetching the user information
return <div />
}
let roles = this.props.roles
if (!roles || roles.includes(user.role)) {
return <Route {...this.props} />
} else if (!!user.role && user.role === 'broker') {
return <Redirect to='/broker-dashboard' />
} else if (!!user.role && (user.role === 'employee' || 'administrator' || 'executive')) {
return <Redirect to='/dashboard' />
}
} else {
return <Redirect to={`/login?redirect=${this.props.location.pathname}${this.props.location.search}`} />
}
}
}