diff --git a/client/src/app/page.tsx b/client/src/app/page.tsx index bb89e2a..ea47ce6 100644 --- a/client/src/app/page.tsx +++ b/client/src/app/page.tsx @@ -1,10 +1,11 @@ -import PublicGames from "@/components/home/PublicGames"; +import PublicGames from "@/components/home/PublicGames/PublicGames"; import JoinGame from "@/components/home/JoinGame"; import CreateGame from "@/components/home/CreateGame"; export default function Home() { return (
+ {/* @ts-expect-error Server Component */}
diff --git a/client/src/components/home/PublicGames.tsx b/client/src/components/home/PublicGames.tsx deleted file mode 100644 index 9dd7b6f..0000000 --- a/client/src/components/home/PublicGames.tsx +++ /dev/null @@ -1,51 +0,0 @@ -import { IconRefresh } from "@tabler/icons-react"; - -export default function PublicGames() { - return ( -
-

- Public games{" "} -
- -
-

- -
- - - - - - - - - - - - - - - {/* - - - - */} - {/* - - - - */} - -
HostOpponent
(empty)
nizenize - -
WPlaceholder_TextWPlaceholder_Text - -
-
-
- ); -} diff --git a/client/src/components/home/PublicGames/JoinButton.tsx b/client/src/components/home/PublicGames/JoinButton.tsx new file mode 100644 index 0000000..1f3a866 --- /dev/null +++ b/client/src/components/home/PublicGames/JoinButton.tsx @@ -0,0 +1,27 @@ +"use client"; + +import { useRouter } from "next/navigation"; +import { useTransition } from "react"; + +export default function JoinButton({ code }: { code: string }) { + const router = useRouter(); + const [isLoading, startTransition] = useTransition(); + + function handleJoin() { + startTransition(() => { + router.push(`/game/${code}`); + }); + } + + return ( + + ); +} diff --git a/client/src/components/home/PublicGames/PublicGames.tsx b/client/src/components/home/PublicGames/PublicGames.tsx new file mode 100644 index 0000000..61378e6 --- /dev/null +++ b/client/src/components/home/PublicGames/PublicGames.tsx @@ -0,0 +1,48 @@ +import { getPublicGames } from "@/lib/game"; +import JoinButton from "./JoinButton"; +import RefreshButton from "./RefreshButton"; + +export default async function PublicGames() { + const games = await getPublicGames(); + + return ( +
+

+ Public games +

+ +
+ + + + + + + + + + {games && games.length > 0 ? ( + games.map((game) => ( + + + + + + )) + ) : ( + + + + + + )} + +
HostOpponent
{game.host?.name} + {(game.host?.id === game.white?.id ? game.black?.name : game.white?.name) || ""} + + +
(empty)
+
+
+ ); +} diff --git a/client/src/components/home/PublicGames/RefreshButton.tsx b/client/src/components/home/PublicGames/RefreshButton.tsx new file mode 100644 index 0000000..bc87b79 --- /dev/null +++ b/client/src/components/home/PublicGames/RefreshButton.tsx @@ -0,0 +1,25 @@ +"use client"; + +import { IconRefresh } from "@tabler/icons-react"; +import { useRouter } from "next/navigation"; +import { useTransition } from "react"; + +export default function RefreshButton() { + const router = useRouter(); + const [isLoading, startTransition] = useTransition(); + + function handleRefresh() { + startTransition(() => { + router.refresh(); + }); + } + + return ( + + ); +} diff --git a/client/src/lib/game.ts b/client/src/lib/game.ts index 8ab6df9..93fd532 100644 --- a/client/src/lib/game.ts +++ b/client/src/lib/game.ts @@ -34,3 +34,16 @@ export const getGame = async (code: string) => { console.error(err); } }; + +export const getPublicGames = async () => { + try { + const res = await fetch(`${API_URL}/v1/games`, { cache: "no-store" }); + + if (res && res.status === 200) { + const games: Game[] = await res.json(); + return games; + } + } catch (err) { + console.error(err); + } +};