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