chore: use monorepo organization
This commit is contained in:
parent
deaf7d6ecc
commit
a687827f7e
1099 changed files with 8184 additions and 11535 deletions
147
packages/admin-ui/src/routing/routes.jsx
Normal file
147
packages/admin-ui/src/routing/routes.jsx
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
import Fade from '@mui/material/Fade'
|
||||
import Slide from '@mui/material/Slide'
|
||||
import * as R from 'ramda'
|
||||
import React, { useContext } from 'react'
|
||||
import {
|
||||
matchPath,
|
||||
Redirect,
|
||||
Switch,
|
||||
useHistory,
|
||||
useLocation
|
||||
} from 'react-router-dom'
|
||||
import Login from 'src/pages/Authentication/Login'
|
||||
import Register from 'src/pages/Authentication/Register'
|
||||
import Reset2FA from 'src/pages/Authentication/Reset2FA'
|
||||
import ResetPassword from 'src/pages/Authentication/ResetPassword'
|
||||
|
||||
import AppContext from 'src/AppContext'
|
||||
import Dashboard from 'src/pages/Dashboard'
|
||||
import Machines from 'src/pages/Machines'
|
||||
import Wizard from 'src/pages/Wizard'
|
||||
|
||||
import PrivateRoute from './PrivateRoute'
|
||||
import PublicRoute from './PublicRoute'
|
||||
import getLamassuRoutes from './lamassu.routes'
|
||||
|
||||
const wrapperClasses = 'flex flex-1 flex-col h-full'
|
||||
|
||||
const tree = getLamassuRoutes()
|
||||
|
||||
const map = R.map(R.when(R.has('children'), R.prop('children')))
|
||||
const mappedRoutes = R.compose(R.flatten, map)(tree)
|
||||
const parentRoutes = R.filter(R.has('children'))(mappedRoutes).concat(
|
||||
R.filter(R.has('children'))(tree)
|
||||
)
|
||||
const leafRoutes = R.compose(R.flatten, map)(mappedRoutes)
|
||||
|
||||
const flattened = R.concat(leafRoutes, parentRoutes)
|
||||
|
||||
const hasSidebar = route =>
|
||||
R.any(r => r.route === route)(
|
||||
R.compose(
|
||||
R.flatten,
|
||||
R.map(R.prop('children')),
|
||||
R.filter(R.has('children'))
|
||||
)(mappedRoutes)
|
||||
)
|
||||
|
||||
const getParent = route =>
|
||||
R.find(
|
||||
R.propEq(
|
||||
'route',
|
||||
R.dropLast(
|
||||
1,
|
||||
R.dropLastWhile(x => x !== '/', route)
|
||||
)
|
||||
)
|
||||
)(flattened)
|
||||
|
||||
const Routes = () => {
|
||||
const history = useHistory()
|
||||
const location = useLocation()
|
||||
const { wizardTested, userData } = useContext(AppContext)
|
||||
|
||||
const dontTriggerPages = [
|
||||
'/404',
|
||||
'/register',
|
||||
'/wizard',
|
||||
'/login',
|
||||
'/register',
|
||||
'/resetpassword',
|
||||
'/reset2fa'
|
||||
]
|
||||
|
||||
if (!wizardTested && !R.contains(location.pathname)(dontTriggerPages)) {
|
||||
history.push('/wizard')
|
||||
return null
|
||||
}
|
||||
|
||||
const getFilteredRoutes = () => {
|
||||
if (!userData) return []
|
||||
|
||||
return flattened.filter(value => {
|
||||
const keys = value.allowedRoles
|
||||
return R.includes(userData.role, keys)
|
||||
})
|
||||
}
|
||||
|
||||
const Transition = location.state ? Slide : Fade
|
||||
|
||||
const transitionProps =
|
||||
Transition === Slide
|
||||
? {
|
||||
direction:
|
||||
R.findIndex(R.propEq('route', location.state.prev))(leafRoutes) >
|
||||
R.findIndex(R.propEq('route', location.pathname))(leafRoutes)
|
||||
? 'right'
|
||||
: 'left'
|
||||
}
|
||||
: { timeout: 400 }
|
||||
|
||||
return (
|
||||
<Switch>
|
||||
<PrivateRoute exact path="/">
|
||||
<Redirect to={{ pathname: '/dashboard' }} />
|
||||
</PrivateRoute>
|
||||
<PrivateRoute path={'/dashboard'}>
|
||||
<Transition
|
||||
className={wrapperClasses}
|
||||
{...transitionProps}
|
||||
in={true}
|
||||
mountOnEnter
|
||||
unmountOnExit>
|
||||
<div className={wrapperClasses}>
|
||||
<Dashboard />
|
||||
</div>
|
||||
</Transition>
|
||||
</PrivateRoute>
|
||||
<PrivateRoute path="/machines" component={Machines} />
|
||||
<PrivateRoute path="/wizard" component={Wizard} />
|
||||
<PublicRoute path="/register" component={Register} />
|
||||
<PublicRoute path="/login" restricted component={Login} />
|
||||
<PublicRoute path="/resetpassword" component={ResetPassword} />
|
||||
<PublicRoute path="/reset2fa" component={Reset2FA} />
|
||||
{getFilteredRoutes().map(({ route, component: Page, key }) => (
|
||||
<PrivateRoute path={route} key={key}>
|
||||
<Transition
|
||||
className={wrapperClasses}
|
||||
{...transitionProps}
|
||||
in={!!matchPath(location.pathname, { path: route })}
|
||||
mountOnEnter
|
||||
unmountOnExit>
|
||||
<div className={wrapperClasses}>
|
||||
<PrivateRoute path={route} key={key}>
|
||||
<Page name={key} />
|
||||
</PrivateRoute>
|
||||
</div>
|
||||
</Transition>
|
||||
</PrivateRoute>
|
||||
))}
|
||||
<PublicRoute path="/404" />
|
||||
<PublicRoute path="*">
|
||||
<Redirect to={{ pathname: '/404' }} />
|
||||
</PublicRoute>
|
||||
</Switch>
|
||||
)
|
||||
}
|
||||
export { tree, getParent, hasSidebar, Routes }
|
||||
Loading…
Add table
Add a link
Reference in a new issue