39-message2opponent (#40)

Reviewed-on: http://192.168.8.55:3000/HQLAx/CordaCheckers/pulls/40
This commit is contained in:
djmil 2023-11-14 11:54:15 +01:00
parent 04f0b86527
commit c969007436
7 changed files with 121 additions and 32 deletions

View File

@ -22,7 +22,13 @@ export default function useGamesApi(gamesReducer, config) {
pushNewGame: (reqParams) => doPushing('/api/gameproposal', 'POST', reqParams, { pushNewGame: (reqParams) => doPushing('/api/gameproposal', 'POST', reqParams, {
onPushing: (isPushingNewGame) => dispatchGames({ type: 'next', isPushingNewGame }), onPushing: (isPushingNewGame) => dispatchGames({ type: 'next', isPushingNewGame }),
onSuccess: (game) => dispatchGames({ type: 'next', gamesList: [...games.gamesList, game] }) onSuccess: (game) => dispatchGames({ type: 'next', gamesList: [...games.gamesList, game], newGame: emptyNewGame })
}), }),
} }
}
const emptyNewGame = {
whitePlayer: '',
blackPlayer: '',
message2opponent: ''
} }

View File

@ -16,6 +16,7 @@ import Backward from './games/action/Backward';
import Forward from './games/action/Forward'; import Forward from './games/action/Forward';
import GameBoard from './games/GameBoard'; import GameBoard from './games/GameBoard';
import Message2Opponent from './games/Message2Opponent';
import './Games.css'; import './Games.css';
@ -27,14 +28,15 @@ export default function Games({ context: { gamesReducer, gamesApi }, players })
<div className='Games'> <div className='Games'>
<div className='left-side'> <div className='left-side'>
<ViewSelector /> <ViewSelector />
<ViewProvider players={players} dispatchGames={dispatchGames} /> <ViewProvider gamesReducer={gamesReducer} players={players} />
</div> </div>
<div className='right-side'> <div className='right-side'>
<ActionPanel players={players} gamesApi={gamesApi} /> <ActionPanel gamesApi={gamesApi} players={players} />
<GameBoard /> <GameBoard />
<Message2Opponent dispatchGames={dispatchGames} />
{/* {/*
<GameMessage /> <GameMessage />
<Message2Opponent /> */} */}
</div> </div>
</div > </div >
</GamesContext.Provider> </GamesContext.Provider>
@ -54,15 +56,19 @@ function ViewSelector() {
) )
} }
function ViewProvider({ players, dispatchGames }) { function ViewProvider({ gamesReducer, players }) {
const [/*games*/, dispatchGames] = gamesReducer;
return ( return (
<div className='ViewProvider'> <div className='ViewProvider'>
<Routes> <Routes>
<Route path='new' element={ <Route path='new' element={
<NewGame <NewGame
onSelectPlayer={(whitePlayer, blackPlayer) => {
dispatchGames({ type: 'nextNewGame', whitePlayer, blackPlayer })
}}
players={players} players={players}
onSelectPlayer={(whitePlayer, blackPlayer) => dispatchGames({ type: "next", newGame: { whitePlayer, blackPlayer } })}
/> />
} /> } />

View File

@ -0,0 +1,48 @@
import React, { useContext, useRef, useState } from 'react';
import { useLocation, matchPath } from 'react-router-dom';
import { GamesContext } from '../../context/games';
export default function Message2Opponent({ dispatchGames }) {
const games = useContext(GamesContext);
const { pathname } = useLocation();
const [value, setValue] = useState('');
const syncTimeoutRef = useRef(null);
if (syncTimeoutRef.current === null) { // <<--- Absorb external value if there is no scheduled sync
if (matchPath('/games/new', pathname)) {
if (value !== games.newGame.message2opponent)
setValue(games.newGame.message2opponent);
} else if (value !== '') {
setValue('');
}
}
/* --- */
const sync = (message2opponent) => {
syncTimeoutRef.current = null;
if (matchPath('/games/new', pathname))
return dispatchGames({ type: 'nextNewGame', message2opponent });
console.warn('unknown path');
}
const update = (value) => {
setValue(value);
if (syncTimeoutRef.current)
clearTimeout(syncTimeoutRef.current); // <<--- Cancel previous sync
syncTimeoutRef.current = setTimeout(() => sync(value), 500);
}
return (
<input className='Message2Opponent'
placeholder='Message to opponent'
value={value}
maxLength={150}
onChange={e => update(e.target.value)}
/>
)
}

View File

@ -29,7 +29,7 @@ export default function Create({ isCurrentUser, pushNewGame }) {
opponentName, opponentName,
opponentColor, opponentColor,
board: null, // default board configuration board: null, // default board configuration
message: 'default NewGame req message' message: games.newGame.message2opponent
} }
pushNewGame(reqParams); pushNewGame(reqParams);

View File

@ -0,0 +1,46 @@
import { useRef, useEffect } from 'react';
import { useLocation } from 'react-router-dom';
export default function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
/* Usage example
const useLocationChange = (action) => {
const location = useLocation();
const prevLocation = usePrevious(location);
useEffect(() => {
action(location, prevLocation);
}, [location]);
}
*/
// runs action(location) on location, i.e. route, change
export const useLocationChange = (action) => {
const location = useLocation();
const prevLocation = usePrevious(location);
useEffect(() => {
if (location !== prevLocation)
action(location, prevLocation);
}, [action, location, prevLocation]);
}
/*
* Usage example
*/
// const MyComponent1 = () => {
// useLocationChange((location) => {
// console.log('handle route change here', location)
// });
// // other code
// }

View File

@ -1,24 +0,0 @@
import { useRef, useEffect } from 'react';
export default function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
/* Usage example
const useLocationChange = (action) => {
const location = useLocation();
const prevLocation = usePrevious(location);
useEffect(() => {
action(location, prevLocation);
}, [location]);
}
*/

View File

@ -6,7 +6,8 @@ const initialState = {
newGame: { newGame: {
whitePlayer: '', whitePlayer: '',
blackPlayer: '' blackPlayer: '',
message2opponent: '',
}, },
// Network // Network
@ -20,6 +21,12 @@ function reducer(state, action) {
case 'next': case 'next':
return nextState(state, action); return nextState(state, action);
case 'nextNewGame':
return {
...state,
newGame: nextState(state.newGame, action)
};
default: default:
throw Error('GamesReducer: unknown action.type', action.type); throw Error('GamesReducer: unknown action.type', action.type);
} }