nquest/frontend/src/App.jsx

93 lines
2.6 KiB
React
Raw Normal View History

2024-01-21 00:50:52 +03:00
import { Navigate, Route, RouterProvider, createBrowserRouter, createRoutesFromElements, useLocation, useRouteLoaderData } from 'react-router-dom'
import PropTypes from 'prop-types'
2023-11-01 23:21:12 +03:00
2024-01-21 01:46:45 +03:00
import { UserProvider } from './store/user'
import { ajax } from './utils/fetch'
2023-11-01 23:21:12 +03:00
import Layout from './components/Layout'
import Index from './pages/Index'
import Login from './pages/Login'
import Register from './pages/Register'
import NoMatch from './pages/NoMatch'
import Engine from './pages/Engine'
2024-01-20 21:37:49 +03:00
import Quests from './pages/Quests'
2024-01-21 01:46:45 +03:00
import User from './pages/User'
2024-01-21 02:20:59 +03:00
import { useRole } from './utils/roles'
import EditQuest from './pages/admin/Quest'
import AdminQuest from './pages/admin/Quests'
2023-11-19 22:54:54 +03:00
2023-11-01 23:21:12 +03:00
const router = createBrowserRouter(
createRoutesFromElements(
<Route
path="/"
id="root"
element={<Layout />}
2024-01-21 00:50:52 +03:00
loader={async () => ajax('/api/user').catch(x => { console.log(x); return null })}
2023-11-01 23:21:12 +03:00
>
2023-11-19 22:54:54 +03:00
<Route
index
element={<Index />}
2024-01-20 21:37:49 +03:00
/>
<Route
id="quests"
path="/quests"
element={<Auth><Quests /></Auth>}
2024-01-21 00:50:52 +03:00
loader={() => ajax('/api/games').catch(x => { console.log(x); return null })}
/>
2024-01-21 01:46:45 +03:00
<Route path="me" element={<User />} />
2023-11-01 23:21:12 +03:00
<Route path="login" element={<Login />} />
<Route path="register" element={<Register />} />
<Route
path="go/:gameId"
element={<Auth><Engine /></Auth>}
2024-01-21 00:50:52 +03:00
loader={({ params }) => ajax(`/api/engine/${params.gameId}`).catch(x => { console.log(x); return null })}
2023-11-01 23:21:12 +03:00
/>
2024-01-21 02:20:59 +03:00
<Route
path="/admin/quests/new"
element={<Auth role="creator"><EditQuest /></Auth>}
2024-01-21 02:20:59 +03:00
// loader={() => ajax(`/api/admin/games`)}
2023-11-19 22:54:54 +03:00
/>
2024-01-21 02:20:59 +03:00
<Route
path="/admin/quests/:gameId"
element={<Auth role="creator"><EditQuest /></Auth>}
loader={({ params }) => ajax(`/api/admin/games/${params.gameId}`)}
/>
<Route
path="/admin/quests"
element={<Auth role="creator"><AdminQuest /></Auth>}
loader={() => ajax('/api/admin/games')}
2024-01-21 02:20:59 +03:00
/>
2023-11-01 23:21:12 +03:00
<Route path="*" element={<NoMatch />} />
</Route>
)
2024-01-21 00:50:52 +03:00
)
function App () {
2023-11-01 23:21:12 +03:00
return (
<RouterProvider router={router} />
)
}
2024-01-21 00:50:52 +03:00
function Auth (props) {
const baseUser = useRouteLoaderData('root')
const { user } = UserProvider.useContainer()
2024-01-21 02:20:59 +03:00
const { hasRole } = useRole()
2024-01-21 00:50:52 +03:00
const location = useLocation()
2023-11-01 23:21:12 +03:00
if (!user && !baseUser) {
2024-01-21 00:50:52 +03:00
return <Navigate to="/login" state={{ from: location }} replace />
2023-11-01 23:21:12 +03:00
}
2024-01-21 02:20:59 +03:00
if (props.role && !hasRole(props.role)) {
2024-01-28 22:19:41 +03:00
return null
2024-01-21 02:20:59 +03:00
}
2024-01-21 00:50:52 +03:00
return props.children
}
Auth.propTypes = {
2024-01-21 02:20:59 +03:00
children: PropTypes.any,
role: PropTypes.string
2023-11-01 23:21:12 +03:00
}
export default App