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) => {
|
|
||||||
|
const nextNewGamePushing = useCallback((isPushing) => {
|
||||||
|
dispatchGuide({ type: 'nextNewGame', isPushing });
|
||||||
|
}, [dispatchGuide]);
|
||||||
|
|
||||||
|
const nextNewGame = useCallback((game) => {
|
||||||
dispatchState({ type: 'add', game });
|
dispatchState({ type: 'add', game });
|
||||||
dispatchGuide({ type: 'nextNewGame', ...gamesGuideTemplate.newGame });
|
dispatchGuide({ type: 'nextNewGame', ...gamesGuideTemplate.newGame });
|
||||||
}
|
}, [dispatchState, dispatchGuide]);
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const pushGameProposalAccept = ({ uuid }) => {
|
const nextGame = useCallback((game) => {
|
||||||
doPushing(`/api/gameproposal/${uuid}/accept`, 'PUT', null, {
|
|
||||||
onPushing: (isPushing) => dispatchGuide({ type: 'UUIDpushing', uuid, what: isPushing && 'accept' }),
|
|
||||||
onSuccess: (game) => dispatchState({ type: 'update', game })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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 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 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 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 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 });
|
dispatchState({ type: 'update', game });
|
||||||
dispatchGuide({ type: 'UUIDmessage', uuid, message: '' });
|
}, [dispatchState]);
|
||||||
},
|
|
||||||
})
|
const nextUUIDpushing = useCallback((uuid, what) => {
|
||||||
}
|
dispatchGuide({ type: 'UUIDpushing', uuid, what });
|
||||||
|
}, [dispatchGuide]);
|
||||||
|
|
||||||
|
const nextUUIDerror = useCallback((uuid, error) => {
|
||||||
|
dispatchGuide({ type: 'UUIDerror', uuid, error });
|
||||||
|
}, [dispatchGuide]);
|
||||||
|
|
||||||
|
const nextUUIDmessage = useCallback((uuid, message) => {
|
||||||
|
dispatchGuide({ type: 'UUIDmessage', uuid, message });
|
||||||
|
}, [dispatchGuide]);
|
||||||
|
|
||||||
|
|
||||||
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