From 6343f70851986dbaf018cc9d972cd279c66f8ba8 Mon Sep 17 00:00:00 2001 From: djmil Date: Wed, 15 Nov 2023 18:10:44 +0100 Subject: [PATCH] GameSlector split into thre sub components --- webapp/src/container/Games.jsx | 25 +++-- .../src/container/games/view/GameSelector.jsx | 92 +++++++++++++++++-- webapp/src/reducer/games.js | 21 +++++ 3 files changed, 117 insertions(+), 21 deletions(-) diff --git a/webapp/src/container/Games.jsx b/webapp/src/container/Games.jsx index 804297e..8558afb 100644 --- a/webapp/src/container/Games.jsx +++ b/webapp/src/container/Games.jsx @@ -3,7 +3,7 @@ import { GamesContext } from '../context/games'; import { NavLink, Routes, Route } from 'react-router-dom'; import NewGame from './games/view/NewGame'; -import GameSelector from './games/view/GameSelector'; +import { GameProposalSelector, ActiveGameSelector, GameArchiveSelector } from './games/view/GameSelector'; import Create from './games/action/Create'; import Reject from './games/action/Reject'; @@ -58,30 +58,29 @@ function ViewSelector({ games }) { } function ViewProvider({ gamesReducer, players }) { - const [games, dispatchGames] = gamesReducer; + const [/*games*/, dispatchGames] = gamesReducer; return (
dispatchGames({ type: 'nextNewGame', whitePlayer, blackPlayer })} + dispatchGames({ type: 'nextNewGame', whitePlayer, blackPlayer })} players={players} /> } /> uuid === games.proposal.selectedUUID} - onSelect={(selectedUUID) => dispatchGames({ type: 'nextProposal', selectedUUID })} - /> + dispatchGames({ type: 'nextProposal', selectedUUID })} /> } /> - } /> - } /> + dispatchGames({ type: 'nextActive', selectedUUID })} /> + } /> + + dispatchGames({ type: 'nextArchive', selectedUUID })} /> + } />
) @@ -99,7 +98,7 @@ function ActionPanel({ players, gamesApi }) { , , - gamesApi.pushGameProposalCancel({ uuid })} /> + gamesApi.pushGameProposalCancel({ uuid })} /> ]} /> , , ]} /> , ]} /> diff --git a/webapp/src/container/games/view/GameSelector.jsx b/webapp/src/container/games/view/GameSelector.jsx index a105e9a..7821f7d 100644 --- a/webapp/src/container/games/view/GameSelector.jsx +++ b/webapp/src/container/games/view/GameSelector.jsx @@ -6,11 +6,13 @@ import { Color, Player } from '../../../components/Checkers'; import Loading from '../../../components/Loading'; import Counter from '../../../components/Counter'; -export default function GameSelector({ yours, opponents, isSelected, onSelect }) { - const gamesList = useContext(GamesContext).gamesList; - if (gamesList === null) +export function GameProposalSelector({ onSelect }) { + const games = useContext(GamesContext); + if (games.gamesList === null) return + const isSelected = (uuid) => uuid === games.proposal.selectedUUID; + const onClick = (uuid) => { if (isSelected(uuid)) onSelect(null); // deselect previously selected game @@ -18,21 +20,95 @@ export default function GameSelector({ yours, opponents, isSelected, onSelect }) onSelect(uuid); } - const yoursList = gamesList.filter(game => game.status === yours) + const yoursList = games.gamesList.filter(game => game.status === 'GAME_PROPOSAL_WAIT_FOR_YOU') .map(game => ) - const opponentsList = gamesList.filter(game => game.status === opponents) + const opponentsList = games.gamesList.filter(game => game.status === 'GAME_PROPOSAL_WAIT_FOR_OPPONENT') .map(game => ) return (
{yoursList} - {opponentsList.length > 0 && } + {opponentsList.length > 0 && waiting for opponent} {opponentsList}
) } +export function ActiveGameSelector({ onSelect }) { + const games = useContext(GamesContext); + if (games.gamesList === null) + return + + const isSelected = (uuid) => uuid === games.proposal.selectedUUID; + + const onClick = (uuid) => { + if (isSelected(uuid)) + onSelect(null); // deselect previously selected game + else + onSelect(uuid); + } + + const yoursList = games.gamesList.filter(game => game.status === 'GAME_BOARD_WAIT_FOR_YOU') + .map(game => ) + + const opponentsList = games.gamesList.filter(game => game.status === 'GAME_BOARD_WAIT_FOR_OPPONENT') + .map(game => ) + + return ( +
+ {yoursList} + {opponentsList.length > 0 && waiting for opponent} + {opponentsList} +
+ ) +} + +export function GameArchiveSelector({ onSelect }) { + const games = useContext(GamesContext); + if (games.gamesList === null) + return + + const isSelected = (uuid) => uuid === games.archive.selectedUUID; + + const onClick = (uuid) => { + if (isSelected(uuid)) + onSelect(null); // deselect previously selected game + else + onSelect(uuid); + } + + const rejectedList = games.gamesList.filter(game => game.status === 'GAME_PROPOSAL_REJECTED') + .map(game => ) + + const canceledList = games.gamesList.filter(game => game.status === 'GAME_PROPOSAL_CANCELED') + .map(game => ) + + const victoryList = games.gamesList.filter(game => game.status === 'GAME_RESULT_YOU_WON') + .map(game => ) + + const defeatList = games.gamesList.filter(game => game.status === 'GAME_RESULT_YOU_LOOSE') + .map(game => ) + + const drawList = games.gamesList.filter(game => game.status === 'GAME_RESULT_DRAW') + .map(game => ) + + return ( +
+ {rejectedList.length > 0 && rejected proposals} + {rejectedList} + {canceledList.length > 0 && canceled proposals} + {canceledList} + {victoryList.length > 0 && victory} + {victoryList} + {defeatList.length > 0 && defeat} + {defeatList} + {drawList.length > 0 && draw} + {drawList} +
+ ) +} + function Selectable({ game, selected, onClick }) { const myColor = game.myColor; const opponentColor = Color.opposite(myColor); @@ -54,10 +130,10 @@ function Selectable({ game, selected, onClick }) { ) }; -function Separator({ counter }) { +function Separator({ counter, children }) { return (
- waiting for opponent + {children}
) diff --git a/webapp/src/reducer/games.js b/webapp/src/reducer/games.js index 4b83cc3..36d0071 100644 --- a/webapp/src/reducer/games.js +++ b/webapp/src/reducer/games.js @@ -15,6 +15,15 @@ export const gamesInitialState = { message2opponent: '', }, + active: { + selectedUUID: null, + message2opponent: '', + }, + + archive: { + selectedUUID: null, + }, + // Network isPollingGamesList: false, isPushingNewGame: false, @@ -42,6 +51,18 @@ function reducer(state, action) { proposal: nextState(state.proposal, action) }; + case 'nextActive': + return { + ...state, + proposal: nextState(state.proposal, action) + }; + + case 'nextArchive': + return { + ...state, + proposal: nextState(state.proposal, action) + }; + default: throw Error('GamesReducer: unknown action.type', action.type); }