diff options
Diffstat (limited to 'frontend/src/App.tsx')
| -rw-r--r-- | frontend/src/App.tsx | 109 |
1 files changed, 69 insertions, 40 deletions
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index bdd3adc..fbfa59f 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx | |||
| @@ -1,32 +1,32 @@ | |||
| 1 | import React from 'react'; | 1 | import React, { useCallback } from "react"; |
| 2 | import { Routes, Route } from "react-router-dom"; | 2 | import { Routes, Route } from "react-router-dom"; |
| 3 | import { Helmet } from "react-helmet"; | 3 | import { Helmet } from "react-helmet"; |
| 4 | 4 | ||
| 5 | import { UserProfile } from '@customTypes/Profile'; | 5 | import { UserProfile } from "@customTypes/Profile"; |
| 6 | import Sidebar from './components/Sidebar'; | 6 | import Sidebar from "./components/Sidebar"; |
| 7 | import "./App.css"; | 7 | import "./App.css"; |
| 8 | 8 | ||
| 9 | import Profile from '@pages/Profile'; | 9 | import Profile from "@pages/Profile"; |
| 10 | import Games from '@pages/Games'; | 10 | import Games from "@pages/Games"; |
| 11 | import Maps from '@pages/Maps'; | 11 | import Maps from "@pages/Maps"; |
| 12 | import User from '@pages/User'; | 12 | import User from "@pages/User"; |
| 13 | import Homepage from '@pages/Homepage'; | 13 | import Homepage from "@pages/Homepage"; |
| 14 | import UploadRunDialog from './components/UploadRunDialog'; | 14 | import UploadRunDialog from "./components/UploadRunDialog"; |
| 15 | import Rules from '@pages/Rules'; | 15 | import Rules from "@pages/Rules"; |
| 16 | import About from '@pages/About'; | 16 | import About from "@pages/About"; |
| 17 | import { Game } from '@customTypes/Game'; | 17 | import { Game } from "@customTypes/Game"; |
| 18 | import { API } from './api/Api'; | 18 | import { API } from "./api/Api"; |
| 19 | import Maplist from '@pages/Maplist'; | 19 | import Maplist from "@pages/Maplist"; |
| 20 | import Rankings from '@pages/Rankings'; | 20 | import Rankings from "@pages/Rankings"; |
| 21 | import { get_user_id_from_token, get_user_mod_from_token } from './utils/Jwt'; | 21 | import { get_user_id_from_token, get_user_mod_from_token } from "./utils/Jwt"; |
| 22 | 22 | ||
| 23 | const App: React.FC = () => { | 23 | const App: React.FC = () => { |
| 24 | const [token, setToken] = React.useState<string | undefined>(undefined); | 24 | const [token, setToken] = React.useState<string | undefined>(undefined); |
| 25 | const [profile, setProfile] = React.useState<UserProfile | undefined>(undefined); | 25 | const [profile, setProfile] = React.useState<UserProfile | undefined>( |
| 26 | undefined | ||
| 27 | ); | ||
| 26 | const [isModerator, setIsModerator] = React.useState<boolean>(false); | 28 | const [isModerator, setIsModerator] = React.useState<boolean>(false); |
| 27 | |||
| 28 | const [games, setGames] = React.useState<Game[]>([]); | 29 | const [games, setGames] = React.useState<Game[]>([]); |
| 29 | |||
| 30 | const [uploadRunDialog, setUploadRunDialog] = React.useState<boolean>(false); | 30 | const [uploadRunDialog, setUploadRunDialog] = React.useState<boolean>(false); |
| 31 | 31 | ||
| 32 | const _fetch_token = async () => { | 32 | const _fetch_token = async () => { |
| @@ -39,12 +39,15 @@ const App: React.FC = () => { | |||
| 39 | setGames(games); | 39 | setGames(games); |
| 40 | }; | 40 | }; |
| 41 | 41 | ||
| 42 | const _set_profile = async (user_id?: string) => { | 42 | const _set_profile = useCallback( |
| 43 | if (user_id && token) { | 43 | async (user_id?: string) => { |
| 44 | const user = await API.get_profile(token); | 44 | if (user_id && token) { |
| 45 | setProfile(user); | 45 | const user = await API.get_profile(token); |
| 46 | } | 46 | setProfile(user); |
| 47 | }; | 47 | } |
| 48 | }, | ||
| 49 | [token] | ||
| 50 | ); | ||
| 48 | 51 | ||
| 49 | React.useEffect(() => { | 52 | React.useEffect(() => { |
| 50 | if (token === undefined) { | 53 | if (token === undefined) { |
| @@ -52,15 +55,15 @@ const App: React.FC = () => { | |||
| 52 | setIsModerator(false); | 55 | setIsModerator(false); |
| 53 | } else { | 56 | } else { |
| 54 | setProfile({} as UserProfile); // placeholder before we call actual user profile | 57 | setProfile({} as UserProfile); // placeholder before we call actual user profile |
| 55 | _set_profile(get_user_id_from_token(token)) | 58 | _set_profile(get_user_id_from_token(token)); |
| 56 | const modStatus = get_user_mod_from_token(token) | 59 | const modStatus = get_user_mod_from_token(token); |
| 57 | if (modStatus) { | 60 | if (modStatus) { |
| 58 | setIsModerator(true); | 61 | setIsModerator(true); |
| 59 | } else { | 62 | } else { |
| 60 | setIsModerator(false); | 63 | setIsModerator(false); |
| 61 | } | 64 | } |
| 62 | } | 65 | } |
| 63 | }, [token]); | 66 | }, [token, _set_profile]); |
| 64 | 67 | ||
| 65 | React.useEffect(() => { | 68 | React.useEffect(() => { |
| 66 | _fetch_token(); | 69 | _fetch_token(); |
| @@ -73,23 +76,49 @@ const App: React.FC = () => { | |||
| 73 | <title>LPHUB</title> | 76 | <title>LPHUB</title> |
| 74 | <meta name="description" content="Least Portals Hub" /> | 77 | <meta name="description" content="Least Portals Hub" /> |
| 75 | </Helmet> | 78 | </Helmet> |
| 76 | <UploadRunDialog token={token} open={uploadRunDialog} onClose={(updateProfile) => { | 79 | <UploadRunDialog |
| 77 | setUploadRunDialog(false); | 80 | token={token} |
| 78 | if (updateProfile) { | 81 | open={uploadRunDialog} |
| 79 | _set_profile(get_user_id_from_token(token)); | 82 | onClose={updateProfile => { |
| 80 | } | 83 | setUploadRunDialog(false); |
| 81 | }} games={games} /> | 84 | if (updateProfile) { |
| 82 | <Sidebar setToken={setToken} profile={profile} setProfile={setProfile} onUploadRun={() => setUploadRunDialog(true)} /> | 85 | _set_profile(get_user_id_from_token(token)); |
| 86 | } | ||
| 87 | }} | ||
| 88 | games={games} | ||
| 89 | /> | ||
| 90 | <Sidebar | ||
| 91 | setToken={setToken} | ||
| 92 | profile={profile} | ||
| 93 | setProfile={setProfile} | ||
| 94 | onUploadRun={() => setUploadRunDialog(true)} | ||
| 95 | /> | ||
| 83 | <Routes> | 96 | <Routes> |
| 84 | <Route path="/" element={<Homepage />} /> | 97 | <Route path="/" element={<Homepage />} /> |
| 85 | <Route path="/profile" element={<Profile profile={profile} token={token} gameData={games} onDeleteRecord={() => _set_profile(get_user_id_from_token(token))} />} /> | 98 | <Route |
| 86 | <Route path="/users/*" element={<User profile={profile} token={token} gameData={games} />} /> | 99 | path="/profile" |
| 100 | element={ | ||
| 101 | <Profile | ||
| 102 | profile={profile} | ||
| 103 | token={token} | ||
| 104 | gameData={games} | ||
| 105 | onDeleteRecord={() => _set_profile(get_user_id_from_token(token))} | ||
| 106 | /> | ||
| 107 | } | ||
| 108 | /> | ||
| 109 | <Route | ||
| 110 | path="/users/*" | ||
| 111 | element={<User profile={profile} token={token} gameData={games} />} | ||
| 112 | /> | ||
| 87 | <Route path="/games" element={<Games games={games} />} /> | 113 | <Route path="/games" element={<Games games={games} />} /> |
| 88 | <Route path='/games/:id' element={<Maplist />}></Route> | 114 | <Route path="/games/:id" element={<Maplist />}></Route> |
| 89 | <Route path="/maps/*" element={<Maps token={token} isModerator={isModerator} />} /> | 115 | <Route |
| 116 | path="/maps/*" | ||
| 117 | element={<Maps token={token} isModerator={isModerator} />} | ||
| 118 | /> | ||
| 90 | <Route path="/rules" element={<Rules />} /> | 119 | <Route path="/rules" element={<Rules />} /> |
| 91 | <Route path="/about" element={<About />} /> | 120 | <Route path="/about" element={<About />} /> |
| 92 | <Route path='/rankings' element={<Rankings />}></Route> | 121 | <Route path="/rankings" element={<Rankings />}></Route> |
| 93 | <Route path="*" element={"404"} /> | 122 | <Route path="*" element={"404"} /> |
| 94 | </Routes> | 123 | </Routes> |
| 95 | </> | 124 | </> |