useCallback for games.API
This commit is contained in:
parent
e70d540632
commit
ea852dfcd2
@ -1,3 +1,4 @@
|
|||||||
|
import { useCallback } from 'react';
|
||||||
import { usePolling, doPushing } from '../hook/api';
|
import { usePolling, doPushing } from '../hook/api';
|
||||||
import { useGamesGuideReducer, useGamesStateReducer, gamesGuideTemplate } from '../reducer/games';
|
import { useGamesGuideReducer, useGamesStateReducer, gamesGuideTemplate } from '../reducer/games';
|
||||||
|
|
||||||
@ -5,98 +6,119 @@ export default function useGamesApi() {
|
|||||||
const [state, dispatchState] = useGamesStateReducer();
|
const [state, dispatchState] = useGamesStateReducer();
|
||||||
const [guide, dispatchGuide] = useGamesGuideReducer();
|
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 }) => {
|
const nextGames = useCallback((games) => {
|
||||||
doPushing('/api/gameproposal', 'POST', { opponentName, opponentColor, board, message }, {
|
dispatchState({ type: 'next', games });
|
||||||
onPushing: (isPushing) => dispatchGuide({ type: 'nextNewGame', isPushing }),
|
}, [dispatchState]);
|
||||||
onSuccess: (game) => {
|
|
||||||
dispatchState({ type: 'add', game });
|
|
||||||
dispatchGuide({ type: 'nextNewGame', ...gamesGuideTemplate.newGame });
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const pushGameProposalAccept = ({ uuid }) => {
|
const nextNewGamePushing = useCallback((isPushing) => {
|
||||||
doPushing(`/api/gameproposal/${uuid}/accept`, 'PUT', null, {
|
dispatchGuide({ type: 'nextNewGame', isPushing });
|
||||||
onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'accept' }),
|
}, [dispatchGuide]);
|
||||||
onSuccess: (game) => dispatchState({ type: 'update', game })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const pushGameProposalReject = ({ uuid }) => {
|
const nextNewGame = useCallback((game) => {
|
||||||
doPushing(`/api/gameproposal/${uuid}/reject`, 'PUT', null, {
|
dispatchState({ type: 'add', game });
|
||||||
onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'reject' }),
|
dispatchGuide({ type: 'nextNewGame', ...gamesGuideTemplate.newGame });
|
||||||
onSuccess: (game) => dispatchState({ type: 'update', game })
|
}, [dispatchState, dispatchGuide]);
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const pushGameProposalCancel = ({ uuid }) => {
|
const nextGame = useCallback((game) => {
|
||||||
doPushing(`/api/gameproposal/${uuid}/cancel`, 'PUT', null, {
|
dispatchState({ type: 'update', game });
|
||||||
onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'cancel' }),
|
}, [dispatchState]);
|
||||||
onSuccess: (game) => dispatchState({ type: 'update', game })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const pushGameSurrender = ({ uuid }) => {
|
const nextUUIDpushing = useCallback((uuid, what) => {
|
||||||
doPushing(`/api/game/${uuid}/surrender`, 'PUT', null, {
|
dispatchGuide({ type: 'UUIDpushing', uuid, what });
|
||||||
onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'surrender' }),
|
}, [dispatchGuide]);
|
||||||
onSuccess: (game) => dispatchState({ type: 'update', game })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const pushGameDrawRequest = ({ uuid }) => {
|
const nextUUIDerror = useCallback((uuid, error) => {
|
||||||
doPushing(`/api/game/${uuid}/drawreq`, 'PUT', null, {
|
dispatchGuide({ type: 'UUIDerror', uuid, error });
|
||||||
onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'draw_request' }),
|
}, [dispatchGuide]);
|
||||||
onSuccess: (game) => dispatchState({ type: 'update', game })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const pushGameDrawAccept = ({ uuid }) => {
|
const nextUUIDmessage = useCallback((uuid, message) => {
|
||||||
doPushing(`/api/game/${uuid}/drawacc`, 'PUT', null, {
|
dispatchGuide({ type: 'UUIDmessage', uuid, message });
|
||||||
onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'draw_accept' }),
|
}, [dispatchGuide]);
|
||||||
onSuccess: (game) => dispatchState({ type: 'update', game })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
return {
|
||||||
state,
|
state,
|
||||||
guide,
|
guide,
|
||||||
dispatchGuide,
|
dispatchGuide,
|
||||||
api: {
|
api: {
|
||||||
useGamesPolling,
|
useGamesPolling: (config) => {
|
||||||
pushNewGame,
|
usePolling('/api/game', {
|
||||||
pushGameProposalAccept,
|
onPolling: (isPolling) => nextPolling(isPolling),
|
||||||
pushGameProposalReject,
|
onSuccess: (games) => nextGames(games)
|
||||||
pushGameProposalCancel,
|
}, config.intervalMode(30));
|
||||||
pushGameSurrender,
|
},
|
||||||
pushGameDrawRequest,
|
|
||||||
pushGameDrawAccept,
|
pushNewGame: ({ opponentName, opponentColor, board, message }) => {
|
||||||
pushGameDrawReject,
|
doPushing('/api/gameproposal', 'POST', { opponentName, opponentColor, board, message }, {
|
||||||
pushGameMove
|
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, '');
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user