export const reducer = (state, action) => { switch (action.update) { case "game-selector": return GameSelector_update(state, action) case "newGame": return updateNewGame(state, action) default: console.warn("Unknown action.component", action.component) return state } } export const initialState = { gameSelector: { selectedGameProposal: null, selectedActiveGame: null, selectedArchiveGame: null, }, newGame: { whitePlayer: '', blackPlayer: '', message: '', fetching: false, }, } 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") } function updateNewGame(state, action) { const newGame = {...state.newGame} 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]) } }) return { ...state, newGame } }