timhaley94/holdem

View on GitHub
client/src/state/round.js

Summary

Maintainability
A
0 mins
Test Coverage
F
0%
import React, {
  createContext,
  useContext,
} from 'react';
import PropTypes from 'prop-types';
import { useGame } from './game';
import { usePlayers } from './players';

const Context = createContext(null);

function RoundProvider({ children }) {
  const { players } = usePlayers();
  const { game, makeMove } = useGame();

  const round = game?.round;
  const value = {
    makeMove,
    round: {
      ...round,
      players: round?.players.map(
        (player) => ({
          ...players.find(
            (p) => p.userId === player.userId,
          ),
          ...player,
        }),
      ),
    },
  };

  return (
    <Context.Provider value={value}>
      { children }
    </Context.Provider>
  );
}

RoundProvider.propTypes = {
  children: PropTypes.node.isRequired,
};

function useRound() {
  return useContext(Context);
}

export {
  RoundProvider,
  useRound,
};