useRoundInfo.jsx 784 B

123456789101112131415161718192021222324
  1. import { useState, useEffect } from 'react';
  2. import { getCurrentRound } from "../domain/apiMethods";
  3. import { useGameId, usePlayerId } from '../domain/gameStore';
  4. export default () => {
  5. const gameId = useGameId();
  6. const playerId = usePlayerId();
  7. const [finished, setFinished] = useState(false);
  8. const [roundInfo, setRoundInfo] = useState({currentRound: null, targetPoint: null, roundSeconds: null});
  9. useEffect(() => {
  10. const setup = async () => {
  11. const { currentRound, coord, timer } = await getCurrentRound(gameId, playerId);
  12. if (currentRound) {
  13. setRoundInfo({ currentRound, targetPoint: coord, roundSeconds: timer });
  14. } else {
  15. setFinished(true);
  16. }
  17. }
  18. setup();
  19. }, [gameId, playerId]);
  20. return [finished, roundInfo];
  21. }