front: GameSelector: store selected game in AppCtx

This commit is contained in:
djmil 2023-10-27 11:32:55 +02:00
parent 15920a666d
commit 10d78d440f
5 changed files with 71 additions and 25 deletions

View File

@ -3,24 +3,40 @@ import React from 'react';
import { useLocation, matchPath } from "react-router"; import { useLocation, matchPath } from "react-router";
import { AppData } from "../../context/data" import { AppData } from "../../context/data"
import { AppContext } from "../../context/app"
import Proposal from './GameSelector/GameProposal'; import Proposal from './GameSelector/GameProposal';
export default function Game() { export default function GameSelector() {
const [data] = React.useContext(AppData) const [data] = React.useContext(AppData)
const [ctx, dispatchCtx] = React.useContext(AppContext)
const { pathname } = useLocation(); const { pathname } = useLocation();
const isProposalPath = matchPath("/game/proposal/*", pathname); const isProposalPath = matchPath("/game/proposal/*", pathname);
const isActivelPath = matchPath("/game/active/*", pathname); const isActivelPath = matchPath("/game/active/*", pathname);
const isArchivePath = matchPath("/game/archive/*", pathname); const isArchivePath = matchPath("/game/archive/*", pathname);
console.log("GameSelector appCtx", ctx)
const onClick_proposal = (selectedGame) => {
dispatchCtx({ component: "game-selector", selectedGameProposal: selectedGame })
}
const onClick_active = (selectedGame) => {
dispatchCtx({ component: "game-selector", selectedActiveGame: selectedGame })
}
const onClick_archive = (selectedGame) => {
dispatchCtx({ component: "game-selector", selectedArchiveGame: selectedGame })
}
if (!data.games) if (!data.games)
return <div>Loading..</div> return <div>Loading..</div>
return ( return (
<div className='game-selector'> <div className='game-selector'>
{isProposalPath && <Proposal games={data.games} />} {isProposalPath && <Proposal games={data.games} onClick={onClick_proposal} />}
{isActivelPath && <div>TBD #1</div>} {isActivelPath && <div>TBD #1</div>}
{isArchivePath && <div>TBD #2</div>} {isArchivePath && <div>TBD #2</div>}
</div> </div>
) )
} }

View File

@ -1,15 +1,15 @@
import React from 'react'; import React from 'react';
import Selectable from './Selectable'; import Selectable from './Selectable';
export default function ProposalSelector({ games }) { export default function ProposalSelector({ games, onClick }) {
const waitForYou = games const waitForYou = games
.filter(game => game.status === Status.WaitForYou) .filter(game => game.status === Status.WaitForYou)
.map(game => <Selectable game={game} key={game.uuid} />) .map(game => <Selectable game={game} key={game.uuid} onClick={onClick} />)
const WaitForOpponent = games const WaitForOpponent = games
.filter(game => game.status === Status.WaitForOpponent) .filter(game => game.status === Status.WaitForOpponent)
.map(game => <Selectable game={game} key={game.uuid} />) .map(game => <Selectable game={game} key={game.uuid} onClick={onClick} />)
return ( return (
<div className="Games"> <div className="Games">

View File

@ -13,6 +13,10 @@
margin-right: 5px; margin-right: 5px;
} }
.selectable:hover {
background-color: #d3d3d360;
}
/* .Games .li button.action { /* .Games .li button.action {
display: none; display: none;
} }

View File

@ -3,7 +3,7 @@ import React from 'react';
import { oppositeColor } from '../Stone'; import { oppositeColor } from '../Stone';
import Player from '../Player'; import Player from '../Player';
export default function Selectable({ game }) { export default function Selectable({ game, onClick }) {
const myColor = game.myColor const myColor = game.myColor
@ -12,7 +12,7 @@ export default function Selectable({ game }) {
return ( return (
<div className='selectable'> <div className='selectable'>
<div className='title'> <div className='title' onClick={() => onClick(game)}>
<Player color={myColor} /> <Player color={myColor} />
<i>vs</i> <i>vs</i>
<Player color={opponentColor} name={opponentName} /> <Player color={opponentColor} name={opponentName} />

View File

@ -1,25 +1,51 @@
export const reducer = (state, action) => { export const reducer = (state, action) => {
switch (action.set) {
case "header": switch (action.component) {
if (action.selected)
return { case "game-selector":
...state, return GameSelector_update(state, action)
header: {
...state.header,
selected: action.selected
}
}
break;
default: default:
console.warn("Unknown action.type", action) console.warn("Unknown action.component", action.component)
return state return state
} }
} }
export const initialState = { export const initialState = {
header: { gameSelector: {
selected: null selectedGameProposal: null,
selectedActiveGame: null,
selectedArchiveGame: null
} }
} }
function GameSelector_update(state, action) {
if (Object.hasOwn(action, 'selectedGameProposal'))
return {
...state,
gameSelector: {
...state.gameSelector,
selectedGameProposal: action.selectedGameProposal
}
}
if (Object.hasOwn(action, 'selectedActiveGame'))
return {
...state,
gameSelector: {
...state.gameSelector,
selectedActiveGame: action.selectedActiveGame
}
}
if (Object.hasOwn(action, 'selectedArchiveGame'))
return {
...state,
gameSelector: {
...state.gameSelector,
selectedArchiveGame: action.selectedArchiveGame
}
}
console.warn(action.component, "- bad property")
}