diff --git a/client/src/components/home/JoinGame.tsx b/client/src/components/home/JoinGame.tsx index 65ec2b7..dd2ec7a 100644 --- a/client/src/components/home/JoinGame.tsx +++ b/client/src/components/home/JoinGame.tsx @@ -3,7 +3,7 @@ import type { FormEvent } from "react"; import { useState, useContext } from "react"; import { SessionContext } from "@/context/session"; -import { findGame } from "@/lib/game"; +import { getGame } from "@/lib/game"; import { useRouter } from "next/navigation"; export default function JoinGame() { @@ -24,7 +24,7 @@ export default function JoinGame() { code = new URL(code).pathname.split("/")[2]; } - const game = await findGame(code); + const game = await getGame(code); if (game) { router.push(`/game/${game.code}`); diff --git a/client/src/lib/game.ts b/client/src/lib/game.ts index 6188416..8ab6df9 100644 --- a/client/src/lib/game.ts +++ b/client/src/lib/game.ts @@ -2,24 +2,35 @@ import type { Game } from "@chessu/types"; import { API_URL } from "@/config"; export const createGame = async (side: string, unlisted: boolean) => { - const res = await fetch(`${API_URL}/v1/games`, { - method: "POST", - credentials: "include", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ side, unlisted }) - }); + try { + const res = await fetch(`${API_URL}/v1/games`, { + method: "POST", + credentials: "include", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ side, unlisted }), + cache: "no-store" + }); - const game: Game | undefined = await res.json(); - return game; -}; - -export const findGame = async (code: string) => { - const res = await fetch(`${API_URL}/v1/games/${code}`); - - if (res && res.status === 200) { - const game: Game = await res.json(); - return game; + if (res && res.status === 201) { + const game: Game = await res.json(); + return game; + } + } catch (err) { + console.error(err); + } +}; + +export const getGame = async (code: string) => { + try { + const res = await fetch(`${API_URL}/v1/games/${code}`, { cache: "no-store" }); + + if (res && res.status === 200) { + const game: Game = await res.json(); + return game; + } + } catch (err) { + console.error(err); } };