summaryrefslogtreecommitdiff
path: root/client/src/Mancala/StartGame.tsx
blob: 4cf8d94ec2279b800ddfd1f1ed1d01811185af2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React, { useState } from "react";
import type { GameState } from "../gameState";
import "./StartGame.css";
import { Play } from "./Play"

type StartGameProps = {
    gameState: GameState | undefined;
    setGameState(newGameState: GameState): void;
}

/**
 * Allows the players to enter their name. A name is required for both players. They can't have the same names.
 */
export function StartGame({gameState, setGameState }: StartGameProps) {

    const [errorMessage, setErrorMessage] = useState("");
    const [playerOne, setPlayerOne] = useState("");
    const [playerTwo, setPlayerTwo] = useState("");

    async function tryStartGame(e: React.FormEvent) {
        e.preventDefault(); // Prevent default browser behavior of submitting forms
        if (!playerOne) {
            setErrorMessage("A name is required for player 1");
            return;
        }
        if (!playerTwo) {
            setErrorMessage("A name is required for player 2");
            return;
        }
        if (playerOne === playerTwo) {
            setErrorMessage("Each player should have a unique name");
            return;
        }
        setErrorMessage("");

        try {
            const response = await fetch('mancala/api/start', {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ nameplayer1: playerOne, nameplayer2: playerTwo })
            });

            if (response.ok) {
                const gameState = await response.json();
                setGameState(gameState);
            } else {
                console.error(response.statusText);
            }
        } catch (error) {
            console.error(error.toString());
        }
    }

    if (localStorage.getItem("state") !== null) {
        var state = localStorage.getItem("state");
        const gameState = JSON.parse(state as string);
        setGameState(gameState);
        return <Play gameState={gameState} setGameState={setGameState} />
    } else {
        return (
            <form onSubmit={(e) => tryStartGame(e)}>
                <input value={playerOne}
                    placeholder="Player 1 name"
                    onChange={(e) => setPlayerOne(e.target.value)}
                />

                <input value={playerTwo}
                    placeholder="Player 2 name"
                    onChange={(e) => setPlayerTwo(e.target.value)}
                />

                <p className="errorMessage">{errorMessage}</p>

                <button className="startGameButton" type="submit">
                    Play Mancala!
                </button>
            </form>
        )
    }
}