Skip to content
Snippets Groups Projects
Commit 2e11e121 authored by Alexandru Munteanu's avatar Alexandru Munteanu
Browse files

refactor admin route

parent c6aca0af
No related branches found
No related tags found
No related merge requests found
...@@ -2,48 +2,26 @@ import React from 'react' ...@@ -2,48 +2,26 @@ import React from 'react'
import { get } from 'lodash' import { get } from 'lodash'
import { compose } from 'recompose' import { compose } from 'recompose'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { Route, Redirect, withRouter } from 'react-router-dom' import { Redirect, withRouter } from 'react-router-dom'
import { actions } from 'pubsweet-client' import { AuthenticatedComponent } from 'pubsweet-client'
const PrivateRoute = ({ const AdminRoute = ({
currentUser, currentUser,
getCurrentUser,
redirectPath = '/', redirectPath = '/',
component: Component, component: Component,
...rest ...rest
}) => ( }) => {
<Route const isAdmin = get(currentUser, 'user.admin')
{...rest} return (
render={props => { <AuthenticatedComponent>
if (!currentUser.user && !currentUser.isFetching) { {isAdmin ? <Component {...rest} /> : <Redirect to="/" />}
getCurrentUser() </AuthenticatedComponent>
return <div>loading</div> )
} }
if (!get(currentUser, 'user.admin') || !currentUser.isAuthenticated) {
return (
<Redirect
to={{
pathname: redirectPath,
state: { from: props.location },
}}
/>
)
}
return <Component {...props} />
}}
/>
)
export default compose( export default compose(
withRouter, withRouter,
connect( connect(state => ({
state => ({ currentUser: state.currentUser,
currentUser: state.currentUser, })),
}), )(AdminRoute)
{
getCurrentUser: actions.getCurrentUser,
},
),
)(PrivateRoute)
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment