player-scores.component.jsx 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import React, { useState,useEffect } from 'react';
  2. import { gameInfo } from '../../services/ggsh.service';
  3. import Loading from '../loading.component';
  4. import PlayerScores from "./player-score-list.component";
  5. const PlayerScoresContainer = ({ gameId, onReturnToStart }) => {
  6. const [scores, setScores] = useState(null);
  7. useEffect(() => {
  8. // define how to fetch scores
  9. const fetchInfo = async () => {
  10. const { players } = await gameInfo(gameId);
  11. setScores(players);
  12. };
  13. // when the component renders, fetch the game info
  14. fetchInfo();
  15. // and do it again every 5 seconds after
  16. const interval = setInterval(() => fetchInfo(), 5000);
  17. // and return a clean-up callback
  18. return () => { clearInterval(interval) };
  19. }, [gameId]);
  20. if (!scores) {
  21. return <Loading/>
  22. }
  23. return (
  24. <div>
  25. <p>Previous Game:</p>
  26. <PlayerScores scores={scores} />
  27. <button onClick={onReturnToStart} >Return to Start</button>
  28. </div>
  29. );
  30. }
  31. export default PlayerScoresContainer;