webserver-framework-frontend/src/App.js

112 lines
3.4 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
2022-11-27 22:21:25 +01:00
import { Navigate, Route, Routes, useNavigate} from 'react-router-dom';
2022-11-17 13:33:59 +01:00
import './css/App.css';
import Home from './pages/Home';
import ErrorBoundary from './util/ErrorBoundary';
import Sidebar, { SidebarMenu } from './components/Sidebar';
import UserControls from './components/UserControls';
import { useLoginContext } from './structures/UserContext';
import Login from './pages/Login';
2022-11-27 22:21:25 +01:00
import { get, setSession, setSettings } from './util/Util';
2022-11-20 17:39:39 +01:00
import PrivateRoute from './structures/PrivateRoute';
import { UnauthedRoute } from './structures/UnauthedRoute';
import Users from './pages/Users';
import Admin from './pages/Admin';
2022-11-24 12:31:30 +01:00
import TitledPage from './components/TitledPage';
2022-11-24 23:12:17 +01:00
import Register from './pages/Register';
function App() {
2022-11-17 13:33:59 +01:00
const [user, updateUser] = useLoginContext();
const [loading, setLoading] = useState(true);
2022-11-27 22:21:25 +01:00
const navigate = useNavigate();
2022-11-17 13:33:59 +01:00
useEffect(() => {
(async () => {
2022-11-27 22:21:25 +01:00
const settings = await get('/api/settings');
setSettings(settings.data);
const result = await get('/api/user');
if (result.status === 200) {
setSession(result.data);
updateUser();
}
setLoading(false);
2022-11-27 22:21:25 +01:00
if (result.data?.twoFactor) return navigate('/login/verify');
})();
2022-11-17 13:33:59 +01:00
}, []);
const menuItems = [
2022-11-25 14:43:55 +01:00
{ to: '/home', label: 'Home', items: [{ to: '/profile', label: 'Profile', relative: true }] },
2022-11-17 13:33:59 +01:00
{ to: '/users', label: 'Users' },
{ to: '/admin', label: 'Admin' }
];
2022-11-17 22:11:52 +01:00
if (loading) return null;
2022-11-17 13:33:59 +01:00
return (
2022-11-22 23:39:43 +01:00
<div className='app is-full-screen'>
2022-11-24 23:12:17 +01:00
2022-11-20 17:39:39 +01:00
<div className='background'>
2022-11-17 13:33:59 +01:00
2022-11-20 17:39:39 +01:00
{user ?
<div>
<header className="card">
<UserControls />
</header>
<Sidebar>
<SidebarMenu menuItems={menuItems} />
</Sidebar>
</div>
2022-11-20 17:39:39 +01:00
: null}
2022-11-17 13:33:59 +01:00
<div className={`main-content ${user ? "" : "login"}`}>
2022-11-24 23:12:17 +01:00
<ErrorBoundary>
<Routes>
<Route path='/home/*' element={<PrivateRoute>
<TitledPage title='Home'>
<Home />
</TitledPage>
</PrivateRoute >} />
<Route path='/users/*' element={<PrivateRoute>
<TitledPage title='Users' >
<Users />
</TitledPage>
</PrivateRoute >} />
<Route path='/admin/*' element={<PrivateRoute>
<Admin />
</PrivateRoute >} />
2022-11-17 13:33:59 +01:00
2022-11-24 23:12:17 +01:00
<Route path='/login/*' element={<UnauthedRoute>
<Login />
</UnauthedRoute>} />
2022-11-17 13:33:59 +01:00
2022-11-24 23:12:17 +01:00
<Route path='/register/*' element={<UnauthedRoute>
<Register />
</UnauthedRoute>} />
<Route path='*' element={<Navigate to='/home' />} />
</Routes>
</ErrorBoundary>
<div className={`footer ${user ? "" : "login"}`}>
<p className='m-0'>Made with by <a href="https://corgi.wtf" target="_blank" rel="noreferrer">Navy.gif</a> &nbsp;|&nbsp; CSS by <a href="https://d3vision.dev" target="_blank" rel="noreferrer">D3vision</a></p>
</div>
2022-11-24 23:12:17 +01:00
</div>
2022-11-17 13:33:59 +01:00
2022-11-24 23:12:17 +01:00
</div>
2022-11-17 13:33:59 +01:00
</div>
);
}
export default App;