34 lines
1.0 KiB
JavaScript
34 lines
1.0 KiB
JavaScript
import { usePolling, doPushing } from '../hook/api';
|
|
|
|
export default function useGamesApi(gamesReducer, config) {
|
|
const [games, dispatchGames] = gamesReducer;
|
|
|
|
const usePollingGamesList = () => {
|
|
const onSuccess = (gamesList) => {
|
|
dispatchGames({ type: 'next', gamesList });
|
|
}
|
|
|
|
const isPollingGamesList = usePolling('/api/games', onSuccess, config.intervalMode(30));
|
|
if (games.isPollingGamesList !== isPollingGamesList) {
|
|
dispatchGames({ type: 'next', isPollingGamesList });
|
|
}
|
|
|
|
return games;
|
|
}
|
|
|
|
|
|
return {
|
|
pollGamesList: usePollingGamesList,
|
|
|
|
pushNewGame: (reqParams) => doPushing('/api/gameproposal', 'POST', reqParams, {
|
|
onPushing: (isPushingNewGame) => dispatchGames({ type: 'next', isPushingNewGame }),
|
|
onSuccess: (game) => dispatchGames({ type: 'next', gamesList: [game, ...games.gamesList], newGame: emptyNewGame })
|
|
}),
|
|
}
|
|
}
|
|
|
|
const emptyNewGame = {
|
|
whitePlayer: '',
|
|
blackPlayer: '',
|
|
message2opponent: ''
|
|
} |