From b6374ed0b64dc2abada10b3b5db412b68b25fa97 Mon Sep 17 00:00:00 2001 From: Toni Barth Date: Tue, 26 Mar 2024 11:48:52 +0100 Subject: [PATCH] add game route without any additional features --- client/src/game.tsx | 34 +++++++++++++++++++++++++++ client/src/lobby.tsx | 24 ++++++++++++++++--- client/src/login.tsx | 7 ++---- client/src/main.tsx | 6 +++++ client/src/navigation.tsx | 21 +++++++++-------- client/src/services/games.service.tsx | 9 +++++++ 6 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 client/src/game.tsx diff --git a/client/src/game.tsx b/client/src/game.tsx new file mode 100644 index 0000000..590df9c --- /dev/null +++ b/client/src/game.tsx @@ -0,0 +1,34 @@ +import { Helmet } from "react-helmet-async" +import type { LoaderFunction } from "react-router" +import { json, useLoaderData } from "react-router-dom" +import { Game as GameEntity } from "./entities" +import GameService from "./services/games.service" + +export const loader: LoaderFunction = async ({ + params, +}): Promise => { + let gs = new GameService() + + let gameId = parseInt(params.gameId as string, 10) + + if (!Number.isNaN(gameId)) { + let game = await gs.get(gameId) + + if (game !== undefined) return game + throw json({ message: "game id not found", status: 404 }) + } + throw json({ message: "internal api error", status: 500 }) +} + +export function Game() { + let game = useLoaderData() as GameEntity + + return ( + <> + + {`Game ${game.id} - Hitster`} + +

Game ID: {game.id}

+ + ) +} diff --git a/client/src/lobby.tsx b/client/src/lobby.tsx index 6babb05..d34dc2d 100644 --- a/client/src/lobby.tsx +++ b/client/src/lobby.tsx @@ -2,7 +2,7 @@ import Button from "react-bootstrap/Button" import Table from "react-bootstrap/Table" import { useCookies } from "react-cookie" import { Helmet } from "react-helmet-async" -import { useLoaderData } from "react-router-dom" +import { Link, useLoaderData, useNavigate } from "react-router-dom" import { Game } from "./entities" import { useRevalidateOnInterval } from "./hooks" import GameService from "./services/games.service" @@ -15,15 +15,29 @@ export async function loader(): Promise { export function Lobby() { let [cookies] = useCookies(["logged_in"]) let games = useLoaderData() as Game[] + let navigate = useNavigate() useRevalidateOnInterval({ enabled: true, interval: 5000 }) + const createGame = async () => { + let res = await fetch("/api/games", { + method: "POST", + credentials: "include", + }) + + if (res.status === 201) + navigate("/game/" + Game.parse(await res.json()).id) + } + return ( <> Game Lobby - Hitster -