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

add admin route

parent 7964a90a
No related branches found
No related tags found
No related merge requests found
import React from 'react'
import { get } from 'lodash'
import { compose } from 'recompose'
import { connect } from 'react-redux'
import { Route, Redirect, withRouter } from 'react-router-dom'
import { getCurrentUser } from 'pubsweet-component-xpub-authentication/src/redux/currentUser'
const PrivateRoute = ({
currentUser,
getCurrentUser,
redirectPath = '/',
component: Component,
...rest
}) => (
<Route
{...rest}
render={props => {
if (!currentUser.isFetched) {
if (!currentUser.isFetching) {
getCurrentUser()
}
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(
withRouter,
connect(
state => ({
currentUser: state.currentUser,
}),
{
getCurrentUser,
},
),
)(PrivateRoute)
export { default as AdminUsers } from './AdminUsers' export { default as AdminUsers } from './AdminUsers'
export { default as Pagination } from './Pagination' export { default as Pagination } from './Pagination'
export { default as AdminRoute } from './AdminRoute'
export { default as AdminDashboard } from './AdminDashboard' export { default as AdminDashboard } from './AdminDashboard'
import React from 'react' import React from 'react'
import { Icon } from '@pubsweet/ui'
import { get } from 'lodash' import { get } from 'lodash'
import { Icon } from '@pubsweet/ui'
import { connect } from 'react-redux'
import styled from 'styled-components' import styled from 'styled-components'
import { withRouter } from 'react-router-dom' import { withRouter } from 'react-router-dom'
import { withState, withHandlers, compose } from 'recompose' import { withState, withHandlers, compose } from 'recompose'
const AppBar = ({ expanded, toggleMenu, brand, user, goTo }) => ( const AppBar = ({ expanded, toggleMenu, brand, user, goTo, currentUser }) => (
<Root> <Root>
{React.cloneElement(brand, { {React.cloneElement(brand, {
onClick: goTo('/'), onClick: goTo('/'),
...@@ -22,9 +23,11 @@ const AppBar = ({ expanded, toggleMenu, brand, user, goTo }) => ( ...@@ -22,9 +23,11 @@ const AppBar = ({ expanded, toggleMenu, brand, user, goTo }) => (
{expanded && ( {expanded && (
<Dropdown> <Dropdown>
<DropdownOption>Settings</DropdownOption> <DropdownOption>Settings</DropdownOption>
<DropdownOption onClick={goTo('admin')}> {currentUser.admin && (
Admin dashboard <DropdownOption onClick={goTo('admin')}>
</DropdownOption> Admin dashboard
</DropdownOption>
)}
<DropdownOption onClick={goTo('/logout')}>Logout</DropdownOption> <DropdownOption onClick={goTo('/logout')}>Logout</DropdownOption>
</Dropdown> </Dropdown>
)} )}
...@@ -34,6 +37,7 @@ const AppBar = ({ expanded, toggleMenu, brand, user, goTo }) => ( ...@@ -34,6 +37,7 @@ const AppBar = ({ expanded, toggleMenu, brand, user, goTo }) => (
</Root> </Root>
) )
// #region styled-components
const Root = styled.div` const Root = styled.div`
align-items: center; align-items: center;
box-shadow: 0 1px 0 0 #667080; box-shadow: 0 1px 0 0 #667080;
...@@ -99,9 +103,13 @@ const ToggleOverlay = styled.div` ...@@ -99,9 +103,13 @@ const ToggleOverlay = styled.div`
right: 0; right: 0;
opacity: 0; opacity: 0;
` `
// #endregion
export default compose( export default compose(
withRouter, withRouter,
connect(state => ({
currentUser: get(state, 'currentUser.user'),
})),
withState('expanded', 'setExpanded', false), withState('expanded', 'setExpanded', false),
withHandlers({ withHandlers({
toggleMenu: ({ setExpanded }) => () => { toggleMenu: ({ setExpanded }) => () => {
......
...@@ -17,6 +17,7 @@ import NotFound from 'pubsweet-components-faraday/src/components/UIComponents/No ...@@ -17,6 +17,7 @@ import NotFound from 'pubsweet-components-faraday/src/components/UIComponents/No
import { import {
AdminDashboard, AdminDashboard,
AdminUsers, AdminUsers,
AdminRoute,
} from 'pubsweet-components-faraday/src/components/Admin' } from 'pubsweet-components-faraday/src/components/Admin'
import AddEditUser from 'pubsweet-components-faraday/src/components/Admin/AddEditUser' import AddEditUser from 'pubsweet-components-faraday/src/components/Admin/AddEditUser'
import SignUpInvitationPage from 'pubsweet-components-faraday/src/components/SignUp/SignUpInvitationPage' import SignUpInvitationPage from 'pubsweet-components-faraday/src/components/SignUp/SignUpInvitationPage'
...@@ -34,10 +35,10 @@ const Routes = () => ( ...@@ -34,10 +35,10 @@ const Routes = () => (
exact exact
path="/confirmation-page" path="/confirmation-page"
/> />
<PrivateRoute component={AdminDashboard} exact path="/admin" /> <AdminRoute component={AdminDashboard} exact path="/admin" />
<PrivateRoute component={AdminUsers} exact path="/admin/users" /> <AdminRoute component={AdminUsers} exact path="/admin/users" />
<PrivateRoute component={AddEditUser} exact path="/admin/users/add" /> <AdminRoute component={AddEditUser} exact path="/admin/users/add" />
<PrivateRoute <AdminRoute
component={AddEditUser} component={AddEditUser}
exact exact
path="/admin/users/edit/:userId" path="/admin/users/edit/:userId"
......
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