App.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React from 'react';
  2. import { getStatus, createGame, gameInfo } from "./services/ggsh.service";
  3. import './App.css';
  4. class InfoComponent extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. loading: true,
  9. version: null,
  10. status: null,
  11. }
  12. }
  13. async componentDidMount() {
  14. const { version, status } = await getStatus();
  15. this.setState({loading: false, version, status});
  16. }
  17. render() {
  18. if (this.state.loading) {
  19. return <p>Connecting to back-end...</p>
  20. }
  21. if (this.state.status !== "healthy") {
  22. return <p>Unable to communicate with API server! Error: {this.state.status}</p>
  23. }
  24. return <p>API Version: {this.state.version}</p>
  25. }
  26. }
  27. const GameInfo = ({game}) => {
  28. const {
  29. gameId,
  30. creator,
  31. timer,
  32. coords,
  33. players
  34. } = game;
  35. return (
  36. <div className="GameInfo">
  37. <p>Game ID: {gameId}</p>
  38. <p>Creator: {creator}</p>
  39. <p>Time Limit: {timer}</p>
  40. <p>Coordinates: {JSON.stringify(coords)}</p>
  41. <p>Players: {JSON.stringify(players)}</p>
  42. </div>
  43. );
  44. }
  45. class GameMakerComp extends React.Component {
  46. constructor(props) {
  47. super(props);
  48. this.state = {
  49. loading: false,
  50. game: null,
  51. }
  52. }
  53. async onClick() {
  54. this.setState({loading: true});
  55. const gameId = await createGame("testing", 300);
  56. const game = await gameInfo(gameId);
  57. this.setState({loading: false, game});
  58. }
  59. render() {
  60. if (this.state.loading) {
  61. return <div><p>Loading...</p></div>
  62. }
  63. if (this.state.game) {
  64. return <div><GameInfo game={this.state.game}/></div>
  65. }
  66. return <div><button onClick={() => this.onClick()}>Create Game!</button></div>
  67. }
  68. }
  69. const App = () => {
  70. return (
  71. <div className="App">
  72. <p>TerrAssumptions!</p>
  73. <hr/>
  74. <GameMakerComp/>
  75. <hr/>
  76. <InfoComponent/>
  77. </div>
  78. );
  79. }
  80. export default App;