diff --git a/webapp/src/api/games.js b/webapp/src/api/games.js index 1cb0d3b..5ee44f2 100644 --- a/webapp/src/api/games.js +++ b/webapp/src/api/games.js @@ -1,3 +1,4 @@ +import { useCallback } from 'react'; import { usePolling, doPushing } from '../hook/api'; import { useGamesGuideReducer, useGamesStateReducer, gamesGuideTemplate } from '../reducer/games'; @@ -5,98 +6,119 @@ export default function useGamesApi() { const [state, dispatchState] = useGamesStateReducer(); const [guide, dispatchGuide] = useGamesGuideReducer(); - const useGamesPolling = (config) => { - const onPolling = (isPolling) => dispatchGuide({ type: 'next', isPolling }); - const onSuccess = (games) => dispatchState({ type: 'next', games }); - usePolling('/api/game', { onPolling, onSuccess }, config.intervalMode(30)); - } + const nextPolling = useCallback((isPolling) => { + dispatchGuide({ type: 'next', isPolling }); + }, [dispatchGuide]); - const pushNewGame = ({ opponentName, opponentColor, board, message }) => { - doPushing('/api/gameproposal', 'POST', { opponentName, opponentColor, board, message }, { - onPushing: (isPushing) => dispatchGuide({ type: 'nextNewGame', isPushing }), - onSuccess: (game) => { - dispatchState({ type: 'add', game }); - dispatchGuide({ type: 'nextNewGame', ...gamesGuideTemplate.newGame }); - } - }) - } + const nextGames = useCallback((games) => { + dispatchState({ type: 'next', games }); + }, [dispatchState]); - const pushGameProposalAccept = ({ uuid }) => { - doPushing(`/api/gameproposal/${uuid}/accept`, 'PUT', null, { - onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'accept' }), - onSuccess: (game) => dispatchState({ type: 'update', game }) - }) - } + const nextNewGamePushing = useCallback((isPushing) => { + dispatchGuide({ type: 'nextNewGame', isPushing }); + }, [dispatchGuide]); - const pushGameProposalReject = ({ uuid }) => { - doPushing(`/api/gameproposal/${uuid}/reject`, 'PUT', null, { - onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'reject' }), - onSuccess: (game) => dispatchState({ type: 'update', game }) - }) - } + const nextNewGame = useCallback((game) => { + dispatchState({ type: 'add', game }); + dispatchGuide({ type: 'nextNewGame', ...gamesGuideTemplate.newGame }); + }, [dispatchState, dispatchGuide]); - const pushGameProposalCancel = ({ uuid }) => { - doPushing(`/api/gameproposal/${uuid}/cancel`, 'PUT', null, { - onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'cancel' }), - onSuccess: (game) => dispatchState({ type: 'update', game }) - }) - } + const nextGame = useCallback((game) => { + dispatchState({ type: 'update', game }); + }, [dispatchState]); - const pushGameSurrender = ({ uuid }) => { - doPushing(`/api/game/${uuid}/surrender`, 'PUT', null, { - onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'surrender' }), - onSuccess: (game) => dispatchState({ type: 'update', game }) - }) - } + const nextUUIDpushing = useCallback((uuid, what) => { + dispatchGuide({ type: 'UUIDpushing', uuid, what }); + }, [dispatchGuide]); - const pushGameDrawRequest = ({ uuid }) => { - doPushing(`/api/game/${uuid}/drawreq`, 'PUT', null, { - onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'draw_request' }), - onSuccess: (game) => dispatchState({ type: 'update', game }) - }) - } + const nextUUIDerror = useCallback((uuid, error) => { + dispatchGuide({ type: 'UUIDerror', uuid, error }); + }, [dispatchGuide]); - const pushGameDrawAccept = ({ uuid }) => { - doPushing(`/api/game/${uuid}/drawacc`, 'PUT', null, { - onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'draw_accept' }), - onSuccess: (game) => dispatchState({ type: 'update', game }) - }) - } + const nextUUIDmessage = useCallback((uuid, message) => { + dispatchGuide({ type: 'UUIDmessage', uuid, message }); + }, [dispatchGuide]); - const pushGameDrawReject = ({ uuid }) => { - doPushing(`/api/game/${uuid}/drawrej`, 'PUT', null, { - onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'draw_reject' }), - onSuccess: (game) => dispatchState({ type: 'update', game }) - }) - } - - const pushGameMove = ({ uuid, move, message }) => { - doPushing(`/api/game/${uuid}/move`, 'PUT', { move, message }, { - onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && { moveFrom: move[0], moveTo: move[1] } }), - onBadReq: (message) => dispatchGuide({ type: 'UUIDerror', uuid, error: { message, moveFrom: move[0], moveTo: move[1] } }), - onSuccess: (game) => { - dispatchState({ type: 'update', game }); - dispatchGuide({ type: 'UUIDmessage', uuid, message: '' }); - }, - }) - } return { state, guide, dispatchGuide, api: { - useGamesPolling, - pushNewGame, - pushGameProposalAccept, - pushGameProposalReject, - pushGameProposalCancel, - pushGameSurrender, - pushGameDrawRequest, - pushGameDrawAccept, - pushGameDrawReject, - pushGameMove + useGamesPolling: (config) => { + usePolling('/api/game', { + onPolling: (isPolling) => nextPolling(isPolling), + onSuccess: (games) => nextGames(games) + }, config.intervalMode(30)); + }, + + pushNewGame: ({ opponentName, opponentColor, board, message }) => { + doPushing('/api/gameproposal', 'POST', { opponentName, opponentColor, board, message }, { + onPushing: (isPushing) => nextNewGamePushing(isPushing), + onSuccess: (game) => nextNewGame(game) + }) + }, + + pushGameProposalAccept: ({ uuid }) => { + doPushing(`/api/gameproposal/${uuid}/accept`, 'PUT', null, { + onPushing: (isPushing) => nextUUIDpushing(uuid, isPushing && 'accept'), + onSuccess: (game) => nextGame(game) + }) + }, + + pushGameProposalReject: ({ uuid }) => { + doPushing(`/api/gameproposal/${uuid}/reject`, 'PUT', null, { + onPushing: (isPushing) => nextUUIDpushing(uuid, isPushing && 'reject'), + onSuccess: (game) => nextGame(game) + }) + }, + + pushGameProposalCancel: ({ uuid }) => { + doPushing(`/api/gameproposal/${uuid}/cancel`, 'PUT', null, { + onPushing: (isPushing) => nextUUIDpushing(uuid, isPushing && 'cancel'), + onSuccess: (game) => nextGame(game) + }) + }, + + pushGameSurrender: ({ uuid }) => { + doPushing(`/api/game/${uuid}/surrender`, 'PUT', null, { + onPushing: (isPushing) => nextUUIDpushing(uuid, isPushing && 'surrender'), + onSuccess: (game) => nextGame(game) + }) + }, + + pushGameDrawRequest: ({ uuid }) => { + doPushing(`/api/game/${uuid}/drawreq`, 'PUT', null, { + onPushing: (isPushing) => nextUUIDpushing(uuid, isPushing && 'draw_request'), + onSuccess: (game) => nextGame(game) + }) + }, + + pushGameDrawAccept: ({ uuid }) => { + doPushing(`/api/game/${uuid}/drawacc`, 'PUT', null, { + onPushing: (isPushing) => nextUUIDpushing(uuid, isPushing && 'draw_accept'), + onSuccess: (game) => nextGame(game) + }) + }, + + pushGameDrawReject: ({ uuid }) => { + doPushing(`/api/game/${uuid}/drawrej`, 'PUT', null, { + onPushing: (isPushing) => nextUUIDpushing(uuid, isPushing && 'draw_reject'), + onSuccess: (game) => nextGame(game) + }) + }, + + pushGameMove: ({ uuid, move, message }) => { + doPushing(`/api/game/${uuid}/move`, 'PUT', { move, message }, { + onPushing: (isPushing) => nextUUIDpushing(uuid, isPushing && { moveFrom: move[0], moveTo: move[1] }), + onBadReq: (errMessage) => nextUUIDerror(uuid, { message: errMessage, moveFrom: move[0], moveTo: move[1] }), + onSuccess: (game) => { + nextGame(game); + nextUUIDmessage(uuid, ''); + }, + }) + } } } } \ No newline at end of file