From 89716c89ad7452b1ae381098f9b88d9a919a30cd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leonardo=20Mur=C3=A7a?=
Date: Tue, 22 Nov 2022 21:07:33 -0300
Subject: [PATCH] Add role based lazy loading
---
src/app/AuthenticatedApp.js | 64 ++++---------------------------------
src/app/ProfessorApp.js | 5 +++
src/app/StudentApp.js | 58 +++++++++++++++++++++++++++++++++
src/services/mocks.js | 1 +
4 files changed, 71 insertions(+), 57 deletions(-)
create mode 100644 src/app/ProfessorApp.js
create mode 100644 src/app/StudentApp.js
diff --git a/src/app/AuthenticatedApp.js b/src/app/AuthenticatedApp.js
index 80d66be..0d6b9cf 100644
--- a/src/app/AuthenticatedApp.js
+++ b/src/app/AuthenticatedApp.js
@@ -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 && (
- <>
-
- Olá, {state.user.firstName} 👋
-
- }
- layoutType={layoutType}
- avatarMenuOptions={avatarMenuOptions(navigate, logout)}
- user={state.user}
- />
-
-
-
- } />
- } />
- } />
- } />
-
- } />
-
-
- } />
-
- } />
- } />
- } />
-
-
- >
- )
+ return state && state.user && state.user.role === 'STUDENT' ? (
+
+ ) : (
+
);
}
diff --git a/src/app/ProfessorApp.js b/src/app/ProfessorApp.js
new file mode 100644
index 0000000..a29f32d
--- /dev/null
+++ b/src/app/ProfessorApp.js
@@ -0,0 +1,5 @@
+function ProfessorApp() {
+ return Professor app
;
+}
+
+export default ProfessorApp;
diff --git a/src/app/StudentApp.js b/src/app/StudentApp.js
new file mode 100644
index 0000000..1122807
--- /dev/null
+++ b/src/app/StudentApp.js
@@ -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 (
+ <>
+
+ Olá, {user.firstName} 👋
+
+ }
+ layoutType={layoutType}
+ avatarMenuOptions={avatarMenuOptions(navigate, logout)}
+ user={user}
+ />
+
+
+
+ } />
+ } />
+ } />
+ } />
+
+ } />
+
+
+ } />
+
+ } />
+ } />
+ } />
+
+
+ >
+ );
+}
+
+export default StudentApp;
diff --git a/src/services/mocks.js b/src/services/mocks.js
index fce3b29..a00ebd3 100644
--- a/src/services/mocks.js
+++ b/src/services/mocks.js
@@ -548,6 +548,7 @@ const user = {
course: 0,
termsAgreed: true,
year: 2018,
+ role: 'STUDENT',
};
const authFailure = {