Add role based lazy loading
This commit is contained in:
parent
15a0af96c7
commit
89716c89ad
4 changed files with 71 additions and 57 deletions
|
@ -1,66 +1,16 @@
|
|||
import { Navigate, Route, Routes, useNavigate } from 'react-router-dom';
|
||||
import { Container } from '@mui/system';
|
||||
import { lazy } from 'react';
|
||||
import { useUser } from '../context/user';
|
||||
import { useAuthState } from '../context/auth';
|
||||
|
||||
import MainMenu from '../components/MainMenu';
|
||||
import Home from '../screens/Home';
|
||||
import Information from '../screens/Information';
|
||||
import Calendar from '../screens/Calendar';
|
||||
import useLayoutType from '../hooks/useLayoutType';
|
||||
import Toolbar from '../components/Toolbar';
|
||||
import Classroom from '../screens/Classroom';
|
||||
import Assignment from '../screens/Assignment';
|
||||
import Profile from '../screens/Profile';
|
||||
|
||||
import { avatarMenuOptions, menuOptions } from './data';
|
||||
|
||||
import styles from './styles';
|
||||
const ProfessorApp = lazy(() => import('./ProfessorApp'));
|
||||
const StudentApp = lazy(() => import('./StudentApp'));
|
||||
|
||||
function AuthenticatedApp() {
|
||||
const navigate = useNavigate();
|
||||
const { state } = useUser();
|
||||
const { logout } = useAuthState();
|
||||
const layoutType = useLayoutType();
|
||||
const { container, toolbar } = styles[layoutType];
|
||||
|
||||
return (
|
||||
state &&
|
||||
state.user && (
|
||||
<>
|
||||
<Toolbar
|
||||
title={
|
||||
<p style={toolbar}>
|
||||
Olá, <strong>{state.user.firstName}</strong> 👋
|
||||
</p>
|
||||
}
|
||||
layoutType={layoutType}
|
||||
avatarMenuOptions={avatarMenuOptions(navigate, logout)}
|
||||
user={state.user}
|
||||
/>
|
||||
<Container maxWidth="false" sx={container}>
|
||||
<MainMenu
|
||||
options={menuOptions(state.pathname)}
|
||||
layoutType={layoutType}
|
||||
/>
|
||||
<Routes>
|
||||
<Route path="/home" element={<Home />} />
|
||||
<Route path="/info" element={<Information />} />
|
||||
<Route path="/calendar" element={<Calendar />} />
|
||||
<Route path="/profile" element={<Profile />} />
|
||||
<Route path="/class">
|
||||
<Route path=":id" element={<Classroom />} />
|
||||
</Route>
|
||||
<Route path="/assignment">
|
||||
<Route path=":id" element={<Assignment />} />
|
||||
</Route>
|
||||
<Route path="/login" element={<Navigate to="/home" />} />
|
||||
<Route path="/register" element={<Navigate to="/home" />} />
|
||||
<Route path="/" element={<Navigate to="/home" />} />
|
||||
</Routes>
|
||||
</Container>
|
||||
</>
|
||||
)
|
||||
return state && state.user && state.user.role === 'STUDENT' ? (
|
||||
<StudentApp user={state.user} pathname={state.pathname} />
|
||||
) : (
|
||||
<ProfessorApp />
|
||||
);
|
||||
}
|
||||
|
||||
|
|
5
src/app/ProfessorApp.js
Normal file
5
src/app/ProfessorApp.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
function ProfessorApp() {
|
||||
return <h1>Professor app</h1>;
|
||||
}
|
||||
|
||||
export default ProfessorApp;
|
58
src/app/StudentApp.js
Normal file
58
src/app/StudentApp.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
import { Navigate, Route, Routes, useNavigate } from 'react-router-dom';
|
||||
import { Container } from '@mui/system';
|
||||
import { useAuthState } from '../context/auth';
|
||||
|
||||
import MainMenu from '../components/MainMenu';
|
||||
import Home from '../screens/Home';
|
||||
import Information from '../screens/Information';
|
||||
import Calendar from '../screens/Calendar';
|
||||
import useLayoutType from '../hooks/useLayoutType';
|
||||
import Toolbar from '../components/Toolbar';
|
||||
import Classroom from '../screens/Classroom';
|
||||
import Assignment from '../screens/Assignment';
|
||||
import Profile from '../screens/Profile';
|
||||
|
||||
import { avatarMenuOptions, menuOptions } from './data';
|
||||
|
||||
import styles from './styles';
|
||||
function StudentApp({ user, pathname }) {
|
||||
const navigate = useNavigate();
|
||||
const { logout } = useAuthState();
|
||||
const layoutType = useLayoutType();
|
||||
const { container, toolbar } = styles[layoutType];
|
||||
|
||||
return (
|
||||
<>
|
||||
<Toolbar
|
||||
title={
|
||||
<p style={toolbar}>
|
||||
Olá, <strong>{user.firstName}</strong> 👋
|
||||
</p>
|
||||
}
|
||||
layoutType={layoutType}
|
||||
avatarMenuOptions={avatarMenuOptions(navigate, logout)}
|
||||
user={user}
|
||||
/>
|
||||
<Container maxWidth="false" sx={container}>
|
||||
<MainMenu options={menuOptions(pathname)} layoutType={layoutType} />
|
||||
<Routes>
|
||||
<Route path="/home" element={<Home />} />
|
||||
<Route path="/info" element={<Information />} />
|
||||
<Route path="/calendar" element={<Calendar />} />
|
||||
<Route path="/profile" element={<Profile />} />
|
||||
<Route path="/class">
|
||||
<Route path=":id" element={<Classroom />} />
|
||||
</Route>
|
||||
<Route path="/assignment">
|
||||
<Route path=":id" element={<Assignment />} />
|
||||
</Route>
|
||||
<Route path="/login" element={<Navigate to="/home" />} />
|
||||
<Route path="/register" element={<Navigate to="/home" />} />
|
||||
<Route path="/" element={<Navigate to="/home" />} />
|
||||
</Routes>
|
||||
</Container>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default StudentApp;
|
|
@ -548,6 +548,7 @@ const user = {
|
|||
course: 0,
|
||||
termsAgreed: true,
|
||||
year: 2018,
|
||||
role: 'STUDENT',
|
||||
};
|
||||
|
||||
const authFailure = {
|
||||
|
|
Loading…
Reference in a new issue