|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { useParams } from "next/navigation"; |
4 | | -import MainNavbar from "@/components/shared/main-navbar"; |
5 | | -import Footer from "@/components/shared/footer"; |
| 3 | +import { useParams, useRouter } from "next/navigation"; |
6 | 4 | import { useQuery } from "@tanstack/react-query"; |
7 | 5 | import { |
8 | | - ContestMatchResult, |
9 | 6 | getContestMatchInfo, |
| 7 | + getMatchmakingTree, |
10 | 8 | } from "@/controllers/contest.controller"; |
11 | 9 | import { MeshGradient } from "@/layouts/mesh-gradient"; |
12 | | -import { ContestantsCards } from "@/components/league/contest/ContestantsCards"; |
13 | | -import { ContestInstructions } from "@/components/league/contest/ContestInstructions"; |
| 10 | +import { ContestantsCards } from "@/components/league/contest/contestants-cards"; |
| 11 | +import { ContestInstructions } from "@/components/league/contest/contest-instructions"; |
14 | 12 | import MatchmakingTree from "@/components/league/matchmaking-tree"; |
15 | | - |
16 | | -const navLinks = [ |
17 | | - { key: "home", label: "Inicio", href: "/" }, |
18 | | - { key: "league", label: "Liga", href: "/league" }, |
19 | | - { key: "rank", label: "Ranking", href: "/rank" }, |
20 | | -]; |
| 13 | +import { ContestMatchResult } from "@/models/contest.model"; |
| 14 | +import { useEffect } from "react"; |
| 15 | +import LogoLoader from "@/components/shared/ui/logo-loader/loader"; |
| 16 | +import { ContestFailedLoad } from "@/components/league/contest/contest-failed-load"; |
| 17 | +import { useContestMatch } from "@/hooks/use-contest-match"; |
21 | 18 |
|
22 | 19 | export default function ContestDetailPage() { |
23 | 20 | const params = useParams(); |
| 21 | + const router = useRouter(); |
24 | 22 | const contestId = params.contestId as string; |
25 | 23 |
|
26 | 24 | const { data, isLoading } = useQuery({ |
27 | 25 | queryKey: ["matchmaking", contestId], |
28 | 26 | queryFn: async () => getContestMatchInfo(Number(contestId)), |
29 | 27 | }); |
30 | 28 |
|
| 29 | + const { data: tree, isLoading: isLoadingTree } = useQuery({ |
| 30 | + queryKey: ["matchmaking-tree", contestId], |
| 31 | + queryFn: async () => getMatchmakingTree(Number(contestId)), |
| 32 | + }); |
| 33 | + |
| 34 | + const [user_ready, toggleUserReady, codeforces_problem, opponent] = |
| 35 | + useContestMatch(Number(contestId), data?.current_student); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + if (!isLoading && data?.msg === ContestMatchResult.NO_LOGGED) { |
| 39 | + router.replace("/log-in"); |
| 40 | + } |
| 41 | + }, [data, isLoading, router]); |
| 42 | + |
| 43 | + if (isLoading || isLoadingTree) { |
| 44 | + return ( |
| 45 | + <MeshGradient> |
| 46 | + <div className="w-screen h-screen flex items-center justify-center"> |
| 47 | + <LogoLoader size={300} /> |
| 48 | + </div> |
| 49 | + </MeshGradient> |
| 50 | + ); |
| 51 | + } |
| 52 | + |
31 | 53 | return ( |
32 | 54 | <> |
33 | | - <MainNavbar navLinks={navLinks} /> |
34 | 55 | <MeshGradient> |
35 | | - <div className="flex flex-col gap-10 items-center justify-center mt-[8%] mx-[20%]"> |
36 | | - <h1 className="text-white"> |
37 | | - {isLoading ? "Cargando..." : data.contest[0].name} |
38 | | - </h1> |
39 | | - <ContestantsCards /> |
40 | | - <ContestInstructions /> |
41 | | - <h1 className="text-white">Matchmaking</h1> |
42 | | - {!isLoading && ( |
43 | | - <MatchmakingTree tree={data.tree} students={data.students} /> |
44 | | - )} |
45 | | - </div> |
| 56 | + {!data.ok || !tree ? ( |
| 57 | + <ContestFailedLoad |
| 58 | + msg={!tree ? ContestMatchResult.NO_TREE : data.msg} |
| 59 | + /> |
| 60 | + ) : ( |
| 61 | + <div className="flex flex-col gap-10 items-center justify-center mt-[8%] mx-[20%]"> |
| 62 | + <h1 className="text-black dark:text-white"> |
| 63 | + {data.contest[0].name} |
| 64 | + </h1> |
| 65 | + <ContestantsCards |
| 66 | + user={{ ...data.current_student, ready: user_ready }} |
| 67 | + oponent={opponent} |
| 68 | + /> |
| 69 | + <ContestInstructions |
| 70 | + ready={user_ready} |
| 71 | + codeforces_problem={codeforces_problem} |
| 72 | + toggleReady={toggleUserReady} |
| 73 | + /> |
| 74 | + <h1 className="text-black dark:text-white">Matchmaking</h1> |
| 75 | + {!isLoadingTree && ( |
| 76 | + <MatchmakingTree tree={tree} students={data.students} /> |
| 77 | + )} |
| 78 | + </div> |
| 79 | + )} |
46 | 80 | </MeshGradient> |
47 | | - <Footer /> |
48 | 81 | </> |
49 | 82 | ); |
50 | 83 | } |
0 commit comments