player-scores.component.jsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import React from 'react';
  2. import { gameInfo } from '../services/ggsh.service';
  3. import Loading from './loading.component';
  4. // TODO render this as a nice score page
  5. // TODO live updates?
  6. const PlayerScores = ({ scores }) => (
  7. <div style={{
  8. display: "flex",
  9. flexFlow: "row wrap",
  10. justifyContent: "space-around",
  11. alignItems: "center",
  12. alignContent: "space-around"}}>
  13. {
  14. scores
  15. .filter(({ currentRound }) => currentRound === null)
  16. .sort((p1, p2) => p1.totalScore > p2.totalScore ? -1 : (p1.totalScore < p2.totalScore ? 1 : 0))
  17. .map(({ name, guesses, totalScore }) =>
  18. <div style={{flex: 1}}>
  19. <p>{name}</p>
  20. <p>Score: {totalScore}</p>
  21. <ol>
  22. <li>{guesses["1"] && guesses["1"].score}</li>
  23. <li>{guesses["2"] && guesses["2"].score}</li>
  24. <li>{guesses["3"] && guesses["3"].score}</li>
  25. <li>{guesses["4"] && guesses["4"].score}</li>
  26. <li>{guesses["5"] && guesses["5"].score}</li>
  27. </ol>
  28. </div>
  29. )
  30. }
  31. </div>
  32. );
  33. class PlayerScoresContainer extends React.Component {
  34. constructor(props) {
  35. super(props);
  36. this.state = { scores: null }
  37. }
  38. async componentDidMount() {
  39. const { gameId } = this.props;
  40. const { players } = await gameInfo(gameId);
  41. this.setState({ scores: players });
  42. }
  43. render() {
  44. const { scores } = this.state;
  45. if (!scores) {
  46. return <Loading/>
  47. }
  48. const { onReturnToStart } = this.props;
  49. return (
  50. <div>
  51. <p>Previous Game:</p>
  52. <PlayerScores scores={scores} />
  53. <button onClick={onReturnToStart}>
  54. Return to Start
  55. </button>
  56. </div>
  57. );
  58. }
  59. }
  60. export default PlayerScoresContainer;