front: NewGame: radiobutton

player name can be used either for black or white side
This commit is contained in:
djmil 2023-10-28 13:01:54 +02:00
parent 142129a376
commit 2777718cda

View File

@ -16,6 +16,9 @@ export default function NewGame() {
if (!isMyPath) if (!isMyPath)
return return
/*
* Name options
*/
const nameOptions = data.leaderboard const nameOptions = data.leaderboard
? Object.keys(data.leaderboard).map(playerName => { ? Object.keys(data.leaderboard).map(playerName => {
const displayName = data.isCurrentUser(playerName) ? 'You' : playerName const displayName = data.isCurrentUser(playerName) ? 'You' : playerName
@ -29,25 +32,42 @@ export default function NewGame() {
const blackOptions = Array(nameOptions) const blackOptions = Array(nameOptions)
blackOptions.push(<option key='default' value=''>{'black player …'}</option>) blackOptions.push(<option key='default' value=''>{'black player …'}</option>)
/*
* Radiobutton
*/
const radioButton = (whitePlayer, blackPlayer) => {
if (whitePlayer !== '' && whitePlayer === ctx.newGame.blackPlayer) {
blackPlayer = ''
}
if (blackPlayer !== '' && blackPlayer === ctx.newGame.whitePlayer) {
whitePlayer = ''
}
dispatchCtx({ update: "newGame", whitePlayer, blackPlayer })
}
const setWhitePlayer = (name) => { const setWhitePlayer = (name) => {
dispatchCtx({ update: "newGame", whitePlayer: name }) radioButton(name, ctx.newGame.blackPlayer)
} }
const setBlackPlayer = (name) => { const setBlackPlayer = (name) => {
dispatchCtx({ update: "newGame", blackPlayer: name }) radioButton(ctx.newGame.whitePlayer, name)
} }
/*
* Component
*/
return ( return (
<div className='new-game'> <div className='new-game'>
<div> <div>
<WhiteStone/> <WhiteStone />
<SelectPlayer name={ctx.newGame.whitePlayer} setName={setWhitePlayer} nameOptions={whiteOptions} /> <SelectPlayer name={ctx.newGame.whitePlayer} setName={setWhitePlayer} nameOptions={whiteOptions} />
</div> </div>
<div>- vs -</div> <div>- vs -</div>
<div> <div>
<SelectPlayer name={ctx.newGame.blackPlayer} setName={setBlackPlayer} nameOptions={blackOptions} /> <SelectPlayer name={ctx.newGame.blackPlayer} setName={setBlackPlayer} nameOptions={blackOptions} />
<BlackStone/> <BlackStone />
</div> </div>
</div> </div>
) )
} }