[800779d] | 1 | import Professor from "./Pages/Professor";
|
---|
| 2 | import SearchResults from "./Pages/SearchResults";
|
---|
| 3 | import Login from "./Pages/Login";
|
---|
[cae16b5] | 4 | import Registration from "./Pages/Registration";
|
---|
[6eba109] | 5 | import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
|
---|
[800779d] | 6 | import Home from "./Pages/Home";
|
---|
[702ca77] | 7 | import UserDashboard from "./Pages/UserDashboard";
|
---|
[8d83180] | 8 | import Subject from "./Pages/Subject";
|
---|
| 9 | import University from "./Pages/University";
|
---|
| 10 | import Faculty from "./Pages/Faculty";
|
---|
[6eba109] | 11 | import { useEffect, useState, useMemo } from "react";
|
---|
| 12 | import AuthApi from "./api/AuthApi";
|
---|
| 13 | import Cookies from "js-cookie";
|
---|
[8d83180] | 14 | import NotFound from "./Pages/NotFound";
|
---|
[c68150f] | 15 | import Topic from "./Pages/Topic";
|
---|
[af801e3] | 16 | import LoadingSpinner from "./Components/Styled/LoadingSpinner.style";
|
---|
[7cb8c3c] | 17 |
|
---|
| 18 | export default function App() {
|
---|
[6eba109] | 19 | const [auth, setAuth] = useState(false);
|
---|
| 20 | const variableAuth = useMemo(() => ({ auth, setAuth }), [auth]);
|
---|
[702ca77] | 21 | const [authLoaded, setAuthLoaded] = useState(false);
|
---|
[6eba109] | 22 |
|
---|
[702ca77] | 23 | const readCookie = async () => {
|
---|
[6eba109] | 24 | const session = Cookies.get("JSESSIONID");
|
---|
| 25 | if (session) {
|
---|
[cae16b5] | 26 | setAuth(true);
|
---|
[702ca77] | 27 | } else {
|
---|
| 28 | setAuth(false);
|
---|
[6eba109] | 29 | }
|
---|
[702ca77] | 30 | setAuthLoaded(true);
|
---|
[6eba109] | 31 | };
|
---|
| 32 |
|
---|
| 33 | useEffect(() => {
|
---|
[c68150f] | 34 | document.title = "profesori.mk";
|
---|
[6eba109] | 35 | readCookie();
|
---|
| 36 | }, []);
|
---|
| 37 |
|
---|
| 38 | const ProtectedRoute = ({ auth, children }) => {
|
---|
[702ca77] | 39 | if (authLoaded) {
|
---|
| 40 | if (!auth) {
|
---|
| 41 | return <Navigate to="/login" replace />;
|
---|
| 42 | }
|
---|
| 43 | return children;
|
---|
| 44 | } else {
|
---|
[af801e3] | 45 | return <LoadingSpinner/>;
|
---|
[6eba109] | 46 | }
|
---|
| 47 | };
|
---|
| 48 |
|
---|
[7cb8c3c] | 49 | return (
|
---|
[6eba109] | 50 | <AuthApi.Provider value={variableAuth}>
|
---|
| 51 | <BrowserRouter>
|
---|
| 52 | <Routes>
|
---|
[c68150f] | 53 | <Route path="/" element={<Home />}>
|
---|
[6eba109] | 54 | <Route path="login" element={<Login />}></Route>
|
---|
[cae16b5] | 55 | <Route path="registration" element={<Registration />}></Route>
|
---|
[6eba109] | 56 | <Route path="professor">
|
---|
[c68150f] | 57 | <Route path=":professorId" element={<Professor />} />
|
---|
[6eba109] | 58 | </Route>
|
---|
[8d83180] | 59 | <Route path="university/:universityId" element={<University />} />
|
---|
| 60 | <Route path="faculty/:facultyId" element={<Faculty />} />
|
---|
| 61 | <Route path="subject/:subjectId" element={<Subject />} />
|
---|
[c68150f] | 62 | <Route path="topic/:topicId" element={<Topic />} />
|
---|
[6eba109] | 63 | <Route path="search" element={<SearchResults />}></Route>
|
---|
| 64 | <Route
|
---|
[702ca77] | 65 | path="user_dashboard"
|
---|
[6eba109] | 66 | element={
|
---|
[c68150f] | 67 | <ProtectedRoute auth={auth}>{<UserDashboard />}</ProtectedRoute>
|
---|
[6eba109] | 68 | }
|
---|
| 69 | ></Route>
|
---|
[800779d] | 70 | </Route>
|
---|
[8d83180] | 71 | <Route path="*" element={<NotFound />} />
|
---|
[6eba109] | 72 | </Routes>
|
---|
| 73 | </BrowserRouter>
|
---|
| 74 | </AuthApi.Provider>
|
---|
[7cb8c3c] | 75 | );
|
---|
[2998dc4] | 76 | }
|
---|