Skip to content

Commit eaf3272

Browse files
sexta13bbmoz
authored andcommitted
fix route by usertype (#174)
1 parent 50cc629 commit eaf3272

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

client/App.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Loadable from 'react-loadable'
66

77
import Loading from './components/Loading/Loading'
88
import AuthActionCreator from './actions/auth'
9-
import CreateProjectForm from './components/CreateProjectForm/CreateProjectForm'
109
import ForgotPasswordForm from './components/ForgotPasswordForm/ForgotPasswordForm'
1110
import Header from './components/Header/Header'
1211
import Home from './components/Home/Home'
@@ -22,6 +21,11 @@ const ProfileRestrictedLoadable = restricted(Loadable({
2221
loading: Loading
2322
}))
2423

24+
const CreateProjectRestrictedLoadable = restricted(Loadable({
25+
loader: () => import('./components/CreateProjectForm/CreateProjectForm'),
26+
loading: Loading
27+
}))
28+
2529
class App extends Component {
2630
componentDidMount () {
2731
this.props.appLoad()
@@ -34,7 +38,7 @@ class App extends Component {
3438
<Version />
3539
<Switch>
3640
<Route exact path='/' component={Home}/>
37-
<Route path='/create-project' component={CreateProjectForm}/>
41+
<Route path='/create-project' component={CreateProjectRestrictedLoadable}/>
3842
<Route path='/forgot-password' component={ForgotPasswordForm}/>
3943
<Route path='/login' component={LoginForm}/>
4044
<Route path='/profile' component={ProfileRestrictedLoadable}/>

client/components/Restricted/Restricted.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react'
22
import { connect } from 'react-redux'
33
import PropTypes from 'prop-types'
4+
import {default as constants} from '../../constants'
45

56
/**
67
* Higher-order component (HOC) to wrap restricted pages
@@ -21,6 +22,18 @@ export default function (BaseComponent) {
2122
const { history } = params
2223
if (!params.authenticated) {
2324
history.replace({ pathname: '/login' })
25+
} else {
26+
if (!this.routesAvailableByUserType()) {
27+
history.replace({pathname: '/'})
28+
}
29+
}
30+
}
31+
32+
routesAvailableByUserType () {
33+
switch (this.props.location.pathname) {
34+
case '/create-project':
35+
return this.props.user.usertype === constants.USER_TYPE_CONTACT
36+
default: return true
2437
}
2538
}
2639

@@ -31,12 +44,14 @@ export default function (BaseComponent) {
3144

3245
function mapStateToProps (state) {
3346
return {
34-
authenticated: state.auth.authenticated
47+
authenticated: state.auth.authenticated,
48+
user: state.user
3549
}
3650
}
3751

3852
Restricted.propTypes = {
39-
location: PropTypes.object
53+
location: PropTypes.object,
54+
user: PropTypes.object
4055
}
4156

4257
return connect(mapStateToProps)(Restricted)

client/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
USER_TYPE_VOLUNTEER: 'volunteer',
3+
USER_TYPE_CONTACT: 'contact'
4+
}

0 commit comments

Comments
 (0)