diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-10-09 16:34:12 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-10-09 16:34:12 +0300 |
| commit | a7c282ca348c1e8e60559e5c064caee28ba11eec (patch) | |
| tree | 43bd7bdb2bbc80b92b96a14b36c33f0b7df622c9 /frontend/src/App.tsx | |
| parent | Rankings page (diff) | |
| download | lphub-a7c282ca348c1e8e60559e5c064caee28ba11eec.tar.gz lphub-a7c282ca348c1e8e60559e5c064caee28ba11eec.tar.bz2 lphub-a7c282ca348c1e8e60559e5c064caee28ba11eec.zip | |
refactor: so much shit
Diffstat (limited to 'frontend/src/App.tsx')
| -rw-r--r-- | frontend/src/App.tsx | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 3980e1b..3a7fa18 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | import React from 'react'; | 1 | import React from 'react'; |
| 2 | import { Routes, Route } from "react-router-dom"; | 2 | import { Routes, Route, useLocation } from "react-router-dom"; |
| 3 | 3 | ||
| 4 | import { UserProfile } from './types/Profile'; | 4 | import { UserProfile } from './types/Profile'; |
| 5 | import Sidebar from './components/Sidebar'; | 5 | import Sidebar from './components/Sidebar'; |
| @@ -17,29 +17,53 @@ import { Game } from './types/Game'; | |||
| 17 | import { API } from './api/Api'; | 17 | import { API } from './api/Api'; |
| 18 | import Maplist from './pages/Maplist'; | 18 | import Maplist from './pages/Maplist'; |
| 19 | import Rankings from './pages/Rankings'; | 19 | import Rankings from './pages/Rankings'; |
| 20 | import { get_user_id_from_token, get_user_mod_from_token } from './utils/Jwt'; | ||
| 20 | 21 | ||
| 21 | const App: React.FC = () => { | 22 | const App: React.FC = () => { |
| 22 | const [token, setToken] = React.useState<string | undefined>(undefined); | 23 | const [token, setToken] = React.useState<string | undefined>(undefined); |
| 23 | const [profile, setProfile] = React.useState<UserProfile | undefined>(undefined); | 24 | const [profile, setProfile] = React.useState<UserProfile | undefined>(undefined); |
| 24 | const [isModerator, setIsModerator] = React.useState<boolean>(true); | 25 | const [isModerator, setIsModerator] = React.useState<boolean>(false); |
| 25 | 26 | ||
| 26 | const [games, setGames] = React.useState<Game[]>([]); | 27 | const [games, setGames] = React.useState<Game[]>([]); |
| 27 | 28 | ||
| 28 | const [uploadRunDialog, setUploadRunDialog] = React.useState<boolean>(false); | 29 | const [uploadRunDialog, setUploadRunDialog] = React.useState<boolean>(false); |
| 29 | const [uploadRunDialogMapID, setUploadRunDialogMapID] = React.useState<number | undefined>(undefined); | 30 | const [uploadRunDialogMapID, setUploadRunDialogMapID] = React.useState<number | undefined>(undefined); |
| 30 | 31 | ||
| 31 | // React.useEffect(() => { | 32 | const _fetch_token = async () => { |
| 32 | // if (token) { | 33 | const token = await API.get_token(); |
| 33 | // setIsModerator(JSON.parse(atob(token.split(".")[1])).mod) | 34 | setToken(token); |
| 34 | // } | 35 | }; |
| 35 | // }, [token]); | ||
| 36 | 36 | ||
| 37 | const _fetch_games = async () => { | 37 | const _fetch_games = async () => { |
| 38 | const games = await API.get_games(); | 38 | const games = await API.get_games(); |
| 39 | setGames(games); | 39 | setGames(games); |
| 40 | }; | 40 | }; |
| 41 | 41 | ||
| 42 | const _set_profile = async (user_id: string | undefined) => { | ||
| 43 | if (user_id) { | ||
| 44 | setProfile({} as UserProfile); // placeholder before we call actual user profile | ||
| 45 | const user = await API.get_profile(token!); | ||
| 46 | setProfile(user); | ||
| 47 | } | ||
| 48 | }; | ||
| 49 | |||
| 50 | React.useEffect(() => { | ||
| 51 | if (token === undefined) { | ||
| 52 | setProfile(undefined); | ||
| 53 | setIsModerator(false); | ||
| 54 | } else { | ||
| 55 | _set_profile(get_user_id_from_token(token)) | ||
| 56 | const modStatus = get_user_mod_from_token(token) | ||
| 57 | if (modStatus) { | ||
| 58 | setIsModerator(true); | ||
| 59 | } else { | ||
| 60 | setIsModerator(false); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | }, [token]); | ||
| 64 | |||
| 42 | React.useEffect(() => { | 65 | React.useEffect(() => { |
| 66 | _fetch_token(); | ||
| 43 | _fetch_games(); | 67 | _fetch_games(); |
| 44 | }, []); | 68 | }, []); |
| 45 | 69 | ||
| @@ -55,14 +79,14 @@ const App: React.FC = () => { | |||
| 55 | <Sidebar setToken={setToken} profile={profile} setProfile={setProfile} onUploadRun={() => setUploadRunDialog(true)} /> | 79 | <Sidebar setToken={setToken} profile={profile} setProfile={setProfile} onUploadRun={() => setUploadRunDialog(true)} /> |
| 56 | <Routes> | 80 | <Routes> |
| 57 | <Route path="/" element={<Homepage />} /> | 81 | <Route path="/" element={<Homepage />} /> |
| 58 | <Route path="/profile" element={<Profile profile={profile} />} /> | 82 | <Route path="/profile" element={<Profile profile={profile} token={token} gameData={games} />} /> |
| 59 | <Route path="/users/*" element={<User />} /> | 83 | <Route path="/users/*" element={<User profile={profile} token={token} gameData={games} />} /> |
| 60 | <Route path="/games" element={<Games games={games} />} /> | 84 | <Route path="/games" element={<Games games={games} />} /> |
| 61 | <Route path='/games/:id' element={<Maplist />}></Route> | 85 | <Route path='/games/:id' element={<Maplist />}></Route> |
| 62 | <Route path="/maps/*" element={<Maps profile={profile} isModerator={isModerator} onUploadRun={(mapID) => {setUploadRunDialog(true);setUploadRunDialogMapID(mapID)}} />}/> | 86 | <Route path="/maps/*" element={<Maps token={token} isModerator={isModerator} />}/> |
| 63 | <Route path="/rules" element={<Rules />} /> | 87 | <Route path="/rules" element={<Rules />} /> |
| 64 | <Route path="/about" element={<About />} /> | 88 | <Route path="/about" element={<About />} /> |
| 65 | <Route path='/rankings' element={<Rankings></Rankings>}></Route> | 89 | <Route path='/rankings' element={<Rankings />}></Route> |
| 66 | <Route path="*" element={"404"} /> | 90 | <Route path="*" element={"404"} /> |
| 67 | </Routes> | 91 | </Routes> |
| 68 | </> | 92 | </> |