Moved game to it's own page

This commit is contained in:
martin 2023-07-02 18:16:26 +02:00
parent dd21f82734
commit 35089e40d1
3 changed files with 33 additions and 21 deletions

View File

@ -1,6 +1,7 @@
import React from "react";
import {Counter} from "./pages/counter";
import Home from "./pages/home";
import Game from "./pages/game";
const AppRoutes = [
{
@ -11,6 +12,10 @@ const AppRoutes = [
path: "/counter",
element: <Counter/>
},
{
path: "/game",
element: <Game/>,
},
];
export default AppRoutes;

View File

@ -0,0 +1,13 @@
import React from "react";
import {GameComponent} from "../components/gameComponent";
import {useLocation} from "react-router-dom";
import Player from "../game/player";
const Game: Component = () => {
const location = useLocation();
const player = location.state as Player;
return <GameComponent player={player}/>;
};
export default Game;

View File

@ -1,42 +1,36 @@
import React, {FormEvent, useRef, useState} from "react";
import {GameComponent} from "../components/gameComponent";
import React, {useRef} from "react";
import Player from "../game/player";
import Input from "../components/input";
import Dropdown from "../components/dropdown";
import {Colour, getColours} from "../game/colour";
import {useNavigate} from "react-router-dom";
const Home: Component = () => {
const [player, setPlayer] = useState<Player>();
const input = useRef<HTMLInputElement>(null);
const dropdown = useRef<HTMLSelectElement>(null);
const navigate = useNavigate();
function formHandler(e: FormEvent): void {
e.preventDefault();
function formHandler(): void {
if (!input.current || !dropdown.current) return;
setPlayer(new Player({
const player = new Player({
Name: input.current.value,
Colour: dropdown.current.value as Colour,
}));
});
navigate("/game", {state: player});
}
return (
<>
<h1 className={"w-fit mx-auto"}>Pac-Man The Board Game</h1>
{
player ?
/*TODO move to own page*/
<GameComponent player={player}/>
:
<form className={"flex flex-col items-center"} onSubmit={formHandler}>
<p>Enter your name:</p>
<Input ref={input} required/>
<p>Choose a colour:</p>
<Dropdown ref={dropdown} options={getColours()}/>
<br/>
<button type={"submit"}>Find game</button>
</form>
}
<form className={"flex flex-col items-center"} onSubmit={formHandler}>
<p>Enter your name:</p>
<Input ref={input} required/>
<p>Choose a colour:</p>
<Dropdown ref={dropdown} options={getColours()}/>
<br/>
<button type={"submit"}>Find game</button>
</form>
</>
);
};