From dea41ea59c558068078c98dd1758cdba2beceda8 Mon Sep 17 00:00:00 2001 From: Wolfboy248 <121288977+Wolfboy248@users.noreply.github.com> Date: Thu, 5 Sep 2024 16:48:01 +0200 Subject: refactor: maplist page --- frontend/src/pages/Games.tsx | 6 +- frontend/src/pages/Maplist.tsx | 183 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 frontend/src/pages/Maplist.tsx (limited to 'frontend/src/pages') diff --git a/frontend/src/pages/Games.tsx b/frontend/src/pages/Games.tsx index ce3db76..eb7177f 100644 --- a/frontend/src/pages/Games.tsx +++ b/frontend/src/pages/Games.tsx @@ -18,13 +18,9 @@ const Games: React.FC = () => { loaders.forEach((loader) => { (loader as HTMLElement).style.display = "none"; }); - } + } React.useEffect(() => { - document.querySelectorAll(".games-page-item-body").forEach((game, index) => { - game.innerHTML = ""; - }); - _fetch_games(); _page_load(); }, []); diff --git a/frontend/src/pages/Maplist.tsx b/frontend/src/pages/Maplist.tsx new file mode 100644 index 0000000..b1b1664 --- /dev/null +++ b/frontend/src/pages/Maplist.tsx @@ -0,0 +1,183 @@ +import React, { useEffect } from "react"; +import { Link, useLocation, useParams } from "react-router-dom"; + +import "../css/Maplist.css"; +import { API } from "../api/Api"; +import { Game, GameChapters } from "../types/Game"; +import { GameChapter, GamesChapters } from "../types/Chapters"; +import { Map } from "../types/Map"; + +const Maplist: React.FC = () => { + const [game, setGame] = React.useState(null); + const [catNum, setCatNum] = React.useState(0); + const [id, setId] = React.useState(0); + const [category, setCategory] = React.useState(0); + const [load, setLoad] = React.useState(false); + const [currentlySelected, setCurrentlySelected] = React.useState(0); + const [hasClicked, setHasClicked] = React.useState(false); + const [gameChapters, setGameChapters] = React.useState(); + const [curChapter, setCurChapter] = React.useState(); + const [numChapters, setNumChapters] = React.useState(0); + + const [dropdownActive, setDropdownActive] = React.useState("none"); + + const params = useParams<{ id: string }>(); + const location = useLocation(); + + function _update_currently_selected(catNum2: number) { + setCurrentlySelected(catNum2); + setHasClicked(true); + } + + const _fetch_chapters = async (chapter_id: string) => { + const chapters = await API.get_chapters(chapter_id); + setCurChapter(chapters); + } + + const _handle_dropdown_click = () => { + if (dropdownActive == "none") { + setDropdownActive("block"); + } else { + setDropdownActive("none"); + } + } + + // im sorry but im too lazy to fix this right now + useEffect(() => { + // gameID + const gameId = parseFloat(params.id || ""); + setId(gameId); + + // location query params + const queryParams = new URLSearchParams(location.search); + if (queryParams.get("cat")) { + const cat = parseFloat(queryParams.get("cat") || ""); + setCategory(cat); + setCatNum(cat - 1); + } + + const _fetch_game = async () => { + const games = await API.get_games(); + const foundGame = games.find((game) => game.id === gameId); + // console.log(foundGame) + if (foundGame) { + setGame(foundGame); + } + }; + + const _fetch_game_chapters = async () => { + const games_chapters = await API.get_games_chapters(gameId.toString()); + setGameChapters(games_chapters); + setNumChapters(games_chapters.chapters.length); + } + + const _fetch_maps = async () => { + const maps = await API.get_games_maps(gameId.toString()); + setLoad(true); + } + + _fetch_game(); + _fetch_game_chapters(); + _fetch_maps(); + }, []); + + useEffect(() => { + if (gameChapters != undefined) { + _fetch_chapters(gameChapters!.chapters[0].id.toString()); + } + }, [gameChapters]) + + + + return ( +
+
+ + + +
+ {!load ? ( +
+ ) : ( +
+

{game?.name}

+
+
+
+

+ { + game?.category_portals.find( + (obj) => obj.category.id === catNum + 1 + )?.portal_count + } +

+

portals

+
+
+ {game?.category_portals.map((cat, index) => ( + + ))} +
+
+
+ +
+
+
+ {curChapter?.chapter.name.split(" - ")[0]} +
+
+ {curChapter?.chapter.name.split(" - ")[1]} + +
+
+ {gameChapters?.chapters.map((chapter, i) => { + return
{_fetch_chapters(chapter.id.toString()); _handle_dropdown_click()}}>{chapter.name}
+ }) + + } +
+
+
+ {curChapter?.maps.map((map, i) => { + return
+ + {map.name} +
+
+ {map.is_disabled ? map.category_portals[0].portal_count : map.category_portals.find( + (obj) => obj.category.id === catNum + 1 + )?.portal_count} + portals +
+
+
+ Difficulty: +
+
+
+
+
+
+
+
+ +
+ })} +
+
+
+ )} +
+ ); +}; + +export default Maplist; -- cgit v1.2.3