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