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/User.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/User.tsx')
| -rw-r--r-- | frontend/src/pages/User.tsx | 146 |
1 files changed, 60 insertions, 86 deletions
diff --git a/frontend/src/pages/User.tsx b/frontend/src/pages/User.tsx index 1605ada..e3781a3 100644 --- a/frontend/src/pages/User.tsx +++ b/frontend/src/pages/User.tsx | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | import React from 'react'; | 1 | import React from 'react'; |
| 2 | import { useLocation } 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'; |
| @@ -9,8 +9,13 @@ import { API } from '../api/Api'; | |||
| 9 | import { ticks_to_time } from '../utils/Time'; | 9 | import { ticks_to_time } from '../utils/Time'; |
| 10 | import "../css/Profile.css"; | 10 | import "../css/Profile.css"; |
| 11 | 11 | ||
| 12 | const User: React.FC = () => { | 12 | interface UserProps { |
| 13 | const location = useLocation(); | 13 | profile?: UserProfile; |
| 14 | token?: string; | ||
| 15 | gameData: Game[]; | ||
| 16 | } | ||
| 17 | |||
| 18 | const User: React.FC<UserProps> = ({ token, profile, gameData }) => { | ||
| 14 | 19 | ||
| 15 | const [user, setUser] = React.useState<UserProfile | undefined>(undefined); | 20 | const [user, setUser] = React.useState<UserProfile | undefined>(undefined); |
| 16 | 21 | ||
| @@ -18,91 +23,63 @@ const User: React.FC = () => { | |||
| 18 | const [pageNumber, setPageNumber] = React.useState(1); | 23 | const [pageNumber, setPageNumber] = React.useState(1); |
| 19 | const [pageMax, setPageMax] = React.useState(0); | 24 | const [pageMax, setPageMax] = React.useState(0); |
| 20 | 25 | ||
| 21 | const [game, setGame] = React.useState("0") | 26 | const [game, setGame] = React.useState("0"); |
| 22 | const [gameData, setGameData] = React.useState<Game[]>([]); | 27 | const [chapter, setChapter] = React.useState("0"); |
| 23 | const [chapter, setChapter] = React.useState("0") | ||
| 24 | const [chapterData, setChapterData] = React.useState<GameChapters | null>(null); | 28 | const [chapterData, setChapterData] = React.useState<GameChapters | null>(null); |
| 25 | const [maps, setMaps] = React.useState<Map[]>([]); | 29 | const [maps, setMaps] = React.useState<Map[]>([]); |
| 26 | 30 | ||
| 27 | function NavClick() { | 31 | const location = useLocation(); |
| 28 | if (user) { | 32 | const navigate = useNavigate(); |
| 29 | const btn = document.querySelectorAll("#section2 button"); | ||
| 30 | btn.forEach((e) => { (e as HTMLElement).style.backgroundColor = "#2b2e46" }); | ||
| 31 | (btn[navState] as HTMLElement).style.backgroundColor = "#202232"; | ||
| 32 | 33 | ||
| 33 | document.querySelectorAll("section").forEach((e, i) => i >= 2 ? e.style.display = "none" : "") | 34 | const _fetch_user = async () => { |
| 34 | if (navState === 0) { document.querySelectorAll(".profile1").forEach((e) => { (e as HTMLElement).style.display = "block" }); } | 35 | const userID = location.pathname.split("/")[2]; |
| 35 | if (navState === 1) { document.querySelectorAll(".profile2").forEach((e) => { (e as HTMLElement).style.display = "block" }); } | 36 | if (token && profile && profile.profile && profile.steam_id === userID) { |
| 37 | navigate("/profile"); | ||
| 38 | return; | ||
| 36 | } | 39 | } |
| 37 | } | 40 | const userData = await API.get_user(userID); |
| 41 | setUser(userData); | ||
| 42 | }; | ||
| 38 | 43 | ||
| 39 | function UpdateProfile() { | 44 | const _get_game_chapters = async () => { |
| 40 | fetch(`https://lp.ardapektezol.com/api/v1/profile`, { | 45 | if (game !== "0") { |
| 41 | method: 'POST', | 46 | const gameChapters = await API.get_games_chapters(game); |
| 42 | headers: { Authorization: "" } | 47 | setChapterData(gameChapters); |
| 43 | }).then(r => r.json()) | 48 | } else { |
| 44 | .then(d => d.success ? window.alert("profile updated") : window.alert(`Error: ${d.message}`)) | 49 | setPageMax(Math.ceil(user!.records.length / 20)); |
| 45 | } | 50 | setPageNumber(1); |
| 51 | } | ||
| 52 | }; | ||
| 46 | 53 | ||
| 47 | const _fetch_user = async () => { | 54 | const _get_game_maps = async () => { |
| 48 | const userData = await API.get_user(location.pathname.split("/")[2]); | 55 | if (chapter === "0") { |
| 49 | setUser(userData); | 56 | const gameMaps = await API.get_game_maps(game); |
| 57 | setMaps(gameMaps); | ||
| 58 | setPageMax(Math.ceil(gameMaps.length / 20)); | ||
| 59 | setPageNumber(1); | ||
| 60 | } else { | ||
| 61 | const gameChapters = await API.get_chapters(chapter); | ||
| 62 | setMaps(gameChapters.maps); | ||
| 63 | setPageMax(Math.ceil(gameChapters.maps.length / 20)); | ||
| 64 | setPageNumber(1); | ||
| 65 | } | ||
| 50 | }; | 66 | }; |
| 51 | 67 | ||
| 52 | React.useEffect(() => { | 68 | React.useEffect(() => { |
| 53 | fetch("https://lp.ardapektezol.com/api/v1/games") | 69 | _fetch_user(); |
| 54 | .then(r => r.json()) | ||
| 55 | .then(d => { | ||
| 56 | setGameData(d.data) | ||
| 57 | setGame("0") | ||
| 58 | }) | ||
| 59 | |||
| 60 | }, [location]); | 70 | }, [location]); |
| 61 | 71 | ||
| 62 | React.useEffect(() => { | 72 | React.useEffect(() => { |
| 63 | if (user) { | 73 | if (user) { |
| 64 | if (game && game !== "0") { | 74 | _get_game_chapters(); |
| 65 | fetch(`https://lp.ardapektezol.com/api/v1/games/${game}`) | ||
| 66 | .then(r => r.json()) | ||
| 67 | .then(d => { | ||
| 68 | setChapterData(d.data) | ||
| 69 | setChapter("0"); | ||
| 70 | // (document.querySelector('#select-chapter') as HTMLInputElement).value = "0" | ||
| 71 | }) | ||
| 72 | |||
| 73 | } else if (game && game === "0") { | ||
| 74 | setPageMax(Math.ceil(user.records.length / 20)) | ||
| 75 | setPageNumber(1) | ||
| 76 | } | ||
| 77 | } | 75 | } |
| 78 | }, [user, game, location]); | 76 | }, [user, game, location]); |
| 79 | 77 | ||
| 80 | React.useEffect(() => { | 78 | React.useEffect(() => { |
| 81 | _fetch_user(); | 79 | if (user && game !== "0") { |
| 82 | }, [user]); | 80 | _get_game_maps(); |
| 83 | |||
| 84 | React.useEffect(() => { | ||
| 85 | if (game !== "0") { | ||
| 86 | if (chapter === "0") { | ||
| 87 | fetch(`https://lp.ardapektezol.com/api/v1/games/${game}/maps`) | ||
| 88 | .then(r => r.json()) | ||
| 89 | .then(d => { | ||
| 90 | setMaps(d.data.maps); | ||
| 91 | setPageMax(Math.ceil(d.data.maps.length / 20)) | ||
| 92 | setPageNumber(1) | ||
| 93 | }) | ||
| 94 | } else { | ||
| 95 | fetch(`https://lp.ardapektezol.com/api/v1/chapters/${chapter}`) | ||
| 96 | .then(r => r.json()) | ||
| 97 | .then(d => { | ||
| 98 | setMaps(d.data.maps); | ||
| 99 | setPageMax(Math.ceil(d.data.maps.length / 20)) | ||
| 100 | setPageNumber(1) | ||
| 101 | }) | ||
| 102 | |||
| 103 | } | ||
| 104 | } | 81 | } |
| 105 | }, [game, chapter, chapterData]) | 82 | }, [user, game, chapter, location]) |
| 106 | 83 | ||
| 107 | if (!user) { | 84 | if (!user) { |
| 108 | return ( | 85 | return ( |
| @@ -113,19 +90,9 @@ const User: React.FC = () => { | |||
| 113 | return ( | 90 | return ( |
| 114 | <main> | 91 | <main> |
| 115 | <section id='section1' className='profile'> | 92 | <section id='section1' className='profile'> |
| 116 | 93 | <div> | |
| 117 | {user.profile | 94 | <img src={user.avatar_link} alt="profile-image"></img> |
| 118 | ? ( | 95 | </div> |
| 119 | <div id='profile-image' onClick={() => UpdateProfile()}> | ||
| 120 | <img src={user.avatar_link} alt="profile-image"></img> | ||
| 121 | <span>Refresh</span> | ||
| 122 | </div> | ||
| 123 | ) : ( | ||
| 124 | <div> | ||
| 125 | <img src={user.avatar_link} alt="profile-image"></img> | ||
| 126 | </div> | ||
| 127 | )} | ||
| 128 | |||
| 129 | <div id='profile-top'> | 96 | <div id='profile-top'> |
| 130 | <div> | 97 | <div> |
| 131 | <div>{user.user_name}</div> | 98 | <div>{user.user_name}</div> |
| @@ -185,7 +152,14 @@ const User: React.FC = () => { | |||
| 185 | {gameData === null ? <select>error</select> : | 152 | {gameData === null ? <select>error</select> : |
| 186 | 153 | ||
| 187 | <select id='select-game' | 154 | <select id='select-game' |
| 188 | onChange={() => setGame((document.querySelector('#select-game') as HTMLInputElement).value)}> | 155 | onChange={() => { |
| 156 | setGame((document.querySelector('#select-game') as HTMLInputElement).value); | ||
| 157 | setChapter("0"); | ||
| 158 | const chapterSelect = document.querySelector('#select-chapter') as HTMLSelectElement; | ||
| 159 | if (chapterSelect) { | ||
| 160 | chapterSelect.value = "0"; | ||
| 161 | } | ||
| 162 | }}> | ||
| 189 | <option value={0} key={0}>All Scores</option> | 163 | <option value={0} key={0}>All Scores</option> |
| 190 | {gameData.map((e, i) => ( | 164 | {gameData.map((e, i) => ( |
| 191 | <option value={e.id} key={i + 1}>{e.name}</option> | 165 | <option value={e.id} key={i + 1}>{e.name}</option> |
| @@ -238,7 +212,7 @@ const User: React.FC = () => { | |||
| 238 | {r.scores.map((e, i) => (<> | 212 | {r.scores.map((e, i) => (<> |
| 239 | {i !== 0 ? <hr style={{ gridColumn: "1 / span 8" }} /> : ""} | 213 | {i !== 0 ? <hr style={{ gridColumn: "1 / span 8" }} /> : ""} |
| 240 | 214 | ||
| 241 | <span>{r.map_name}</span> | 215 | <Link to={`/maps/${r.map_id}`}><span>{r.map_name}</span></Link> |
| 242 | 216 | ||
| 243 | <span style={{ display: "grid" }}>{e.score_count}</span> | 217 | <span style={{ display: "grid" }}>{e.score_count}</span> |
| 244 | 218 | ||
| @@ -250,7 +224,7 @@ const User: React.FC = () => { | |||
| 250 | <span style={{ flexDirection: "row-reverse" }}> | 224 | <span style={{ flexDirection: "row-reverse" }}> |
| 251 | 225 | ||
| 252 | <button onClick={() => { window.alert(`Demo ID: ${e.demo_id}`) }}><img src={ThreedotIcon} alt="demo_id" /></button> | 226 | <button onClick={() => { window.alert(`Demo ID: ${e.demo_id}`) }}><img src={ThreedotIcon} alt="demo_id" /></button> |
| 253 | <button onClick={() => window.location.href = `https://lp.ardapektezol.com/api/v1/demos?uuid=${e.demo_id}`}><img src={DownloadIcon} alt="download" /></button> | 227 | <button onClick={() => window.location.href = `/api/v1/demos?uuid=${e.demo_id}`}><img src={DownloadIcon} alt="download" /></button> |
| 254 | {i === 0 && r.scores.length > 1 ? <button onClick={() => { | 228 | {i === 0 && r.scores.length > 1 ? <button onClick={() => { |
| 255 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "44px" || | 229 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "44px" || |
| 256 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "" ? | 230 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "" ? |
| @@ -295,7 +269,7 @@ const User: React.FC = () => { | |||
| 295 | <span style={{ flexDirection: "row-reverse" }}> | 269 | <span style={{ flexDirection: "row-reverse" }}> |
| 296 | 270 | ||
| 297 | <button onClick={() => { window.alert(`Demo ID: ${e.demo_id}`) }}><img src={ThreedotIcon} alt="demo_id" /></button> | 271 | <button onClick={() => { window.alert(`Demo ID: ${e.demo_id}`) }}><img src={ThreedotIcon} alt="demo_id" /></button> |
| 298 | <button onClick={() => window.location.href = `https://lp.ardapektezol.com/api/v1/demos?uuid=${e.demo_id}`}><img src={DownloadIcon} alt="download" /></button> | 272 | <button onClick={() => window.location.href = `/api/v1/demos?uuid=${e.demo_id}`}><img src={DownloadIcon} alt="download" /></button> |
| 299 | {i === 0 && record!.scores.length > 1 ? <button onClick={() => { | 273 | {i === 0 && record!.scores.length > 1 ? <button onClick={() => { |
| 300 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "44px" || | 274 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "44px" || |
| 301 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "" ? | 275 | (document.querySelectorAll(".profileboard-record")[index % 20] as HTMLInputElement).style.height === "" ? |