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/pages/Profile.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/pages/Profile.tsx')
| -rw-r--r-- | frontend/src/pages/Profile.tsx | 131 |
1 files changed, 54 insertions, 77 deletions
diff --git a/frontend/src/pages/Profile.tsx b/frontend/src/pages/Profile.tsx index 8eec23c..e20d930 100644 --- a/frontend/src/pages/Profile.tsx +++ b/frontend/src/pages/Profile.tsx | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | import React from 'react'; | 1 | import React from 'react'; |
| 2 | import { useLocation, useNavigate } from 'react-router-dom'; | 2 | import { Link, useLocation, useNavigate } from 'react-router-dom'; |
| 3 | 3 | ||
| 4 | import { SteamIcon, TwitchIcon, YouTubeIcon, PortalIcon, FlagIcon, StatisticsIcon, SortIcon, ThreedotIcon, DownloadIcon, HistoryIcon } from '../images/Images'; | 4 | import { SteamIcon, TwitchIcon, YouTubeIcon, PortalIcon, FlagIcon, StatisticsIcon, SortIcon, ThreedotIcon, DownloadIcon, HistoryIcon } from '../images/Images'; |
| 5 | import { UserProfile } from '../types/Profile'; | 5 | import { UserProfile } from '../types/Profile'; |
| @@ -7,104 +7,74 @@ import { Game, GameChapters } from '../types/Game'; | |||
| 7 | import { Map } from '../types/Map'; | 7 | import { Map } from '../types/Map'; |
| 8 | import { ticks_to_time } from '../utils/Time'; | 8 | import { ticks_to_time } from '../utils/Time'; |
| 9 | import "../css/Profile.css"; | 9 | import "../css/Profile.css"; |
| 10 | import { API } from '../api/Api'; | ||
| 10 | 11 | ||
| 11 | interface ProfileProps { | 12 | interface ProfileProps { |
| 12 | profile?: UserProfile; | 13 | profile?: UserProfile; |
| 14 | token?: string; | ||
| 15 | gameData: Game[]; | ||
| 13 | } | 16 | } |
| 14 | 17 | ||
| 15 | const Profile: React.FC<ProfileProps> = ({ profile }) => { | 18 | const Profile: React.FC<ProfileProps> = ({ profile, token, gameData }) => { |
| 16 | |||
| 17 | |||
| 18 | const location = useLocation(); | ||
| 19 | const navigate = useNavigate(); | ||
| 20 | |||
| 21 | React.useEffect(() => { | ||
| 22 | if (!profile) { | ||
| 23 | navigate("/"); | ||
| 24 | }; | ||
| 25 | }, [profile]); | ||
| 26 | 19 | ||
| 27 | const [navState, setNavState] = React.useState(0); | 20 | const [navState, setNavState] = React.useState(0); |
| 28 | const [pageNumber, setPageNumber] = React.useState(1); | 21 | const [pageNumber, setPageNumber] = React.useState(1); |
| 29 | const [pageMax, setPageMax] = React.useState(0); | 22 | const [pageMax, setPageMax] = React.useState(0); |
| 30 | 23 | ||
| 31 | const [game, setGame] = React.useState("0") | 24 | const [game, setGame] = React.useState("0") |
| 32 | const [gameData, setGameData] = React.useState<Game[]>([]); | ||
| 33 | const [chapter, setChapter] = React.useState("0") | 25 | const [chapter, setChapter] = React.useState("0") |
| 34 | const [chapterData, setChapterData] = React.useState<GameChapters | null>(null); | 26 | const [chapterData, setChapterData] = React.useState<GameChapters | null>(null); |
| 35 | const [maps, setMaps] = React.useState<Map[]>([]); | 27 | const [maps, setMaps] = React.useState<Map[]>([]); |
| 36 | 28 | ||
| 37 | function NavClick() { | 29 | const navigate = useNavigate(); |
| 38 | if (profile) { | ||
| 39 | const btn = document.querySelectorAll("#section2 button"); | ||
| 40 | btn.forEach((e) => { (e as HTMLElement).style.backgroundColor = "#2b2e46" }); | ||
| 41 | (btn[navState] as HTMLElement).style.backgroundColor = "#202232"; | ||
| 42 | 30 | ||
| 43 | document.querySelectorAll("section").forEach((e, i) => i >= 2 ? e.style.display = "none" : "") | 31 | const _update_profile = () => { |
| 44 | if (navState === 0) { document.querySelectorAll(".profile1").forEach((e) => { (e as HTMLElement).style.display = "block" }); } | 32 | if (token) { |
| 45 | if (navState === 1) { document.querySelectorAll(".profile2").forEach((e) => { (e as HTMLElement).style.display = "block" }); } | 33 | API.post_profile(token).then(() => navigate(0)); |
| 46 | } | 34 | } |
| 47 | } | 35 | }; |
| 48 | 36 | ||
| 49 | function UpdateProfile() { | 37 | const _get_game_chapters = async () => { |
| 50 | fetch(`https://lp.ardapektezol.com/api/v1/profile`, { | 38 | if (game && game !== "0") { |
| 51 | method: 'POST', | 39 | const gameChapters = await API.get_games_chapters(game); |
| 52 | headers: { Authorization: "" } | 40 | setChapterData(gameChapters); |
| 53 | }).then(r => r.json()) | 41 | } else if (game && game === "0") { |
| 54 | .then(d => d.success ? window.alert("profile updated") : window.alert(`Error: ${d.message}`)) | 42 | setPageMax(Math.ceil(profile!.records.length / 20)); |
| 55 | } | 43 | setPageNumber(1); |
| 44 | } | ||
| 45 | }; | ||
| 56 | 46 | ||
| 57 | React.useEffect(() => { | 47 | const _get_game_maps = async () => { |
| 58 | if (profile) { | 48 | if (chapter === "0") { |
| 59 | fetch("https://lp.ardapektezol.com/api/v1/games") | 49 | const gameMaps = await API.get_game_maps(game); |
| 60 | .then(r => r.json()) | 50 | setMaps(gameMaps); |
| 61 | .then(d => { | 51 | setPageMax(Math.ceil(gameMaps.length / 20)); |
| 62 | setGameData(d.data) | 52 | setPageNumber(1); |
| 63 | setGame("0") | 53 | } else { |
| 64 | }) | 54 | const gameChapters = await API.get_chapters(chapter); |
| 55 | setMaps(gameChapters.maps); | ||
| 56 | setPageMax(Math.ceil(gameChapters.maps.length / 20)); | ||
| 57 | setPageNumber(1); | ||
| 65 | } | 58 | } |
| 66 | }, [profile, location]); | 59 | }; |
| 60 | |||
| 61 | React.useEffect(() => { | ||
| 62 | if (!profile) { | ||
| 63 | navigate("/"); | ||
| 64 | }; | ||
| 65 | }, [profile]); | ||
| 67 | 66 | ||
| 68 | React.useEffect(() => { | 67 | React.useEffect(() => { |
| 69 | if (profile) { | 68 | if (profile) { |
| 70 | if (game && game !== "0") { | 69 | _get_game_chapters(); |
| 71 | fetch(`https://lp.ardapektezol.com/api/v1/games/${game}`) | ||
| 72 | .then(r => r.json()) | ||
| 73 | .then(d => { | ||
| 74 | setChapterData(d.data) | ||
| 75 | setChapter("0"); | ||
| 76 | // (document.querySelector('#select-chapter') as HTMLInputElement).value = "0" | ||
| 77 | }) | ||
| 78 | |||
| 79 | } else if (game && game === "0") { | ||
| 80 | setPageMax(Math.ceil(profile.records.length / 20)) | ||
| 81 | setPageNumber(1) | ||
| 82 | } | ||
| 83 | } | 70 | } |
| 84 | }, [profile, game, location]); | 71 | }, [profile, game]); |
| 85 | 72 | ||
| 86 | React.useEffect(() => { | 73 | React.useEffect(() => { |
| 87 | if (game !== "0") { | 74 | if (profile && game !== "0") { |
| 88 | if (chapter === "0") { | 75 | _get_game_maps(); |
| 89 | fetch(`https://lp.ardapektezol.com/api/v1/games/${game}/maps`) | ||
| 90 | .then(r => r.json()) | ||
| 91 | .then(d => { | ||
| 92 | setMaps(d.data.maps); | ||
| 93 | setPageMax(Math.ceil(d.data.maps.length / 20)) | ||
| 94 | setPageNumber(1) | ||
| 95 | }) | ||
| 96 | } else { | ||
| 97 | fetch(`https://lp.ardapektezol.com/api/v1/chapters/${chapter}`) | ||
| 98 | .then(r => r.json()) | ||
| 99 | .then(d => { | ||
| 100 | setMaps(d.data.maps); | ||
| 101 | setPageMax(Math.ceil(d.data.maps.length / 20)) | ||
| 102 | setPageNumber(1) | ||
| 103 | }) | ||
| 104 | |||
| 105 | } | ||
| 106 | } | 76 | } |
| 107 | }, [game, chapter, chapterData]) | 77 | }, [profile, game, chapter, chapterData]) |
| 108 | 78 | ||
| 109 | if (!profile) { | 79 | if (!profile) { |
| 110 | return ( | 80 | return ( |
| @@ -118,7 +88,7 @@ const Profile: React.FC<ProfileProps> = ({ profile }) => { | |||
| 118 | 88 | ||
| 119 | {profile.profile | 89 | {profile.profile |
| 120 | ? ( | 90 | ? ( |
| 121 | <div id='profile-image' onClick={() => UpdateProfile()}> | 91 | <div id='profile-image' onClick={_update_profile}> |
| 122 | <img src={profile.avatar_link} alt="profile-image"></img> | 92 | <img src={profile.avatar_link} alt="profile-image"></img> |
| 123 | <span>Refresh</span> | 93 | <span>Refresh</span> |
| 124 | </div> | 94 | </div> |
| @@ -187,7 +157,14 @@ const Profile: React.FC<ProfileProps> = ({ profile }) => { | |||
| 187 | {gameData === null ? <select>error</select> : | 157 | {gameData === null ? <select>error</select> : |
| 188 | 158 | ||
| 189 | <select id='select-game' | 159 | <select id='select-game' |
| 190 | onChange={() => setGame((document.querySelector('#select-game') as HTMLInputElement).value)}> | 160 | onChange={() => { |
| 161 | setGame((document.querySelector('#select-game') as HTMLInputElement).value); | ||
| 162 | setChapter("0"); | ||
| 163 | const chapterSelect = document.querySelector('#select-chapter') as HTMLSelectElement; | ||
| 164 | if (chapterSelect) { | ||
| 165 | chapterSelect.value = "0"; | ||
| 166 | } | ||
| 167 | }}> | ||
| 191 | <option value={0} key={0}>All Scores</option> | 168 | <option value={0} key={0}>All Scores</option> |
| 192 | {gameData.map((e, i) => ( | 169 | {gameData.map((e, i) => ( |
| 193 | <option value={e.id} key={i + 1}>{e.name}</option> | 170 | <option value={e.id} key={i + 1}>{e.name}</option> |
| @@ -240,7 +217,7 @@ const Profile: React.FC<ProfileProps> = ({ profile }) => { | |||
| 240 | {r.scores.map((e, i) => (<> | 217 | {r.scores.map((e, i) => (<> |
| 241 | {i !== 0 ? <hr style={{ gridColumn: "1 / span 8" }} /> : ""} | 218 | {i !== 0 ? <hr style={{ gridColumn: "1 / span 8" }} /> : ""} |
| 242 | 219 | ||
| 243 | <span>{r.map_name}</span> | 220 | <Link to={`/maps/${r.map_id}`}><span>{r.map_name}</span></Link> |
| 244 | 221 | ||
| 245 | <span style={{ display: "grid" }}>{e.score_count}</span> | 222 | <span style={{ display: "grid" }}>{e.score_count}</span> |
| 246 | 223 | ||
| @@ -252,7 +229,7 @@ const Profile: React.FC<ProfileProps> = ({ profile }) => { | |||
| 252 | <span style={{ flexDirection: "row-reverse" }}> | 229 | <span style={{ flexDirection: "row-reverse" }}> |
| 253 | 230 | ||
| 254 | <button onClick={() => { window.alert(`Demo ID: ${e.demo_id}`) }}><img src={ThreedotIcon} alt="demo_id" /></button> | 231 | <button onClick={() => { window.alert(`Demo ID: ${e.demo_id}`) }}><img src={ThreedotIcon} alt="demo_id" /></button> |
| 255 | <button onClick={() => window.location.href = `https://lp.ardapektezol.com/api/v1/demos?uuid=${e.demo_id}`}><img src={DownloadIcon} alt="download" /></button> | 232 | <button onClick={() => window.location.href = `/api/v1/demos?uuid=${e.demo_id}`}><img src={DownloadIcon} alt="download" /></button> |
| 256 | {i === 0 && r.scores.length > 1 ? <button onClick={() => { | 233 | {i === 0 && r.scores.length > 1 ? <button onClick={() => { |
| 257 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "44px" || | 234 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "44px" || |
| 258 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "" ? | 235 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "" ? |
| @@ -297,7 +274,7 @@ const Profile: React.FC<ProfileProps> = ({ profile }) => { | |||
| 297 | <span style={{ flexDirection: "row-reverse" }}> | 274 | <span style={{ flexDirection: "row-reverse" }}> |
| 298 | 275 | ||
| 299 | <button onClick={() => { window.alert(`Demo ID: ${e.demo_id}`) }}><img src={ThreedotIcon} alt="demo_id" /></button> | 276 | <button onClick={() => { window.alert(`Demo ID: ${e.demo_id}`) }}><img src={ThreedotIcon} alt="demo_id" /></button> |
| 300 | <button onClick={() => window.location.href = `https://lp.ardapektezol.com/api/v1/demos?uuid=${e.demo_id}`}><img src={DownloadIcon} alt="download" /></button> | 277 | <button onClick={() => window.location.href = `/api/v1/demos?uuid=${e.demo_id}`}><img src={DownloadIcon} alt="download" /></button> |
| 301 | {i === 0 && record!.scores.length > 1 ? <button onClick={() => { | 278 | {i === 0 && record!.scores.length > 1 ? <button onClick={() => { |
| 302 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "44px" || | 279 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "44px" || |
| 303 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "" ? | 280 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "" ? |