Compare commits

...

2 Commits

Author SHA1 Message Date
142129a376 front: AppCtx: better update function 2023-10-28 12:26:37 +02:00
85cab1e1ae front: NewGame: CSS first level childs only 2023-10-28 11:15:17 +02:00
3 changed files with 23 additions and 28 deletions

View File

@ -6,7 +6,7 @@
width: 230px;
}
.new-game div {
.new-game>div { /* first level childs only*/
margin-top: 25px;
margin-bottom: 25px;
}

View File

@ -24,17 +24,17 @@ export default function NewGame() {
: []
const whiteOptions = Array(nameOptions)
whiteOptions.push(<option key='default' value=''>{'select …'}</option>)
whiteOptions.push(<option key='default' value=''>{'white player …'}</option>)
const blackOptions = Array(nameOptions)
blackOptions.push(<option key='default' value=''>{'select …'}</option>)
blackOptions.push(<option key='default' value=''>{'black player …'}</option>)
const setWhitePlayer = (name) => {
dispatchCtx({ component: "new-game", whitePlayer: name })
dispatchCtx({ update: "newGame", whitePlayer: name })
}
const setBlackPlayer = (name) => {
dispatchCtx({ component: "new-game", blackPlayer: name })
dispatchCtx({ update: "newGame", blackPlayer: name })
}
return (

View File

@ -1,12 +1,12 @@
export const reducer = (state, action) => {
switch (action.component) {
switch (action.update) {
case "game-selector":
return GameSelector_update(state, action)
case "new-game":
return NewGame_update(state, action)
case "newGame":
return updateNewGame(state, action)
default:
console.warn("Unknown action.component", action.component)
@ -62,26 +62,21 @@ function GameSelector_update(state, action) {
console.warn(action.component, "- bad property")
}
function NewGame_update(state, action) {
if (Object.hasOwn(action, 'whitePlayer')) {
return {
...state,
newGame: {
...state.newGame,
whitePlayer: action.whitePlayer
}
}
}
function updateNewGame(state, action) {
const newGame = {...state.newGame}
if (Object.hasOwn(action, 'blackPlayer')) {
return {
...state,
newGame: {
...state.newGame,
blackPlayer: action.blackPlayer
}
Object.keys(action)
.slice(1) // skip 'update' property
.forEach(actionKey => {
if (Object.hasOwn(newGame, actionKey)) {
newGame[actionKey] = action[actionKey]
} else {
console.warn("NewGame update: bad action property\n", actionKey + ":", action[actionKey])
}
}
})
console.warn(action.component, "- bad property")
}
return {
...state,
newGame
}
}