import React, { useState } from "react"; import { PRE_GAME, PRE_ROUND, IN_ROUND, POST_ROUND, POST_GAME, ERROR, } from "./game-state.enum"; import HeaderAndFooter from "../util/header-and-footer.component"; import PreGame from '../screens/pre-game.component'; import PreRound from '../screens/pre-round.component'; import GamePanelContainer from "../screens/game-panel.component"; import RoundSummary from '../screens/round-summary.component'; import PlayerScoresContainer from "../screens/player-scores.component"; const initialState = { gameState: PRE_GAME, gameId: null, playerName: null, lastRound: null, } const Game = () => { const [ state, rawSetState ] = useState(initialState); const setGameState = gameState => rawSetState({ ...state, gameState }); const setGameStateAnd = (gameState, updates) => rawSetState({ ...state, gameState, ...updates }); switch (state.gameState) { case PRE_GAME: return ( setGameStateAnd(PRE_ROUND, { gameId, playerName })} /> ); case PRE_ROUND: return setGameState(IN_ROUND)} /> case IN_ROUND: return setGameStateAnd(POST_ROUND, { lastRound })} onGameEnd={() => setGameState(POST_GAME)} /> case POST_ROUND: return setGameState(IN_ROUND)} /> case POST_GAME: return ( setGameState(PRE_GAME)} /> ); case ERROR: // TODO - would be nice to hook this into the sub-components, maybe with a HOC? return

Application encountered unrecoverable error, please refresh the page.

default: setGameState(ERROR); return

Application state is inconsistent, please refresh and rejoin your previous game.

} } export default Game;