source: reactapp/src/App.js@ af801e3

main
Last change on this file since af801e3 was af801e3, checked in by viktor <viktor@…>, 2 years ago

finished edit/delete/displace opinion/thread from report (react); todo reporting user/opinion/thread interface, public user pages and messaging (springboot)

  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[800779d]1import Professor from "./Pages/Professor";
2import SearchResults from "./Pages/SearchResults";
3import Login from "./Pages/Login";
[cae16b5]4import Registration from "./Pages/Registration";
[6eba109]5import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
[800779d]6import Home from "./Pages/Home";
[702ca77]7import UserDashboard from "./Pages/UserDashboard";
[8d83180]8import Subject from "./Pages/Subject";
9import University from "./Pages/University";
10import Faculty from "./Pages/Faculty";
[6eba109]11import { useEffect, useState, useMemo } from "react";
12import AuthApi from "./api/AuthApi";
13import Cookies from "js-cookie";
[8d83180]14import NotFound from "./Pages/NotFound";
[c68150f]15import Topic from "./Pages/Topic";
[af801e3]16import LoadingSpinner from "./Components/Styled/LoadingSpinner.style";
[7cb8c3c]17
18export 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}
Note: See TracBrowser for help on using the repository browser.