import React, { useState } from "react";
import Loading from "../loading.component";
import { createGame, joinGame } from "../../services/ggsh.service";
// TODO set round timer for new game
const initialState = playerName => ({
loading: false,
gameId: null,
playerName,
});
const PlayerName = ({ playerName, onChangePlayerName }) => (
Player Name
);
const NewGame = ({ onCreateGame, cannotCreateGame }) => (
);
const JoinGame = ({ gameId, onChangeGameId, onJoinGame, cannotJoinGame }) => (
Game ID:
);
const PreGame = ({ initPlayerName, onGameJoined }) => {
const [state, setState] = useState(initialState(initPlayerName));
const { loading, gameId, playerName } = state;
if (loading) {
return
}
const onChangePlayerName = ({ target }) => setState({ ...state, playerName: target.value.trim() });
const onChangeGameId = ({ target }) => setState({ ...state, gameId: target.value.trim() });
const onCreateGame = async () => {
setState({ ...state, loading: true })
const gameId = await createGame(playerName, 300);
onGameJoined({ gameId, playerName });
};
const cannotCreateGame = !playerName || playerName.length === 0;
const onJoinGame = async () => {
// TODO would like to support re-joining a game you left
setState({ ...state, loading: true })
await joinGame(gameId, playerName);
onGameJoined({ gameId, playerName });
};
const cannotJoinGame = cannotCreateGame || !gameId || gameId.length === 0;
return (
);
}
export default PreGame;