Преглед изворни кода

Adding service function for creating game

Kirk Trombley пре 5 година
родитељ
комит
5c8ebee717
2 измењених фајлова са 28 додато и 2 уклоњено
  1. 10 1
      ui/src/App.js
  2. 18 1
      ui/src/services/ggsh.service.js

+ 10 - 1
ui/src/App.js

@@ -1,5 +1,5 @@
 import React from 'react';
-import { getStatus } from "./services/ggsh.service";
+import { getStatus, createGame } from "./services/ggsh.service";
 import './App.css';
 
 class InfoComponent extends React.Component {
@@ -30,11 +30,20 @@ class InfoComponent extends React.Component {
   }
 }
 
+const NewGameButton = () => (
+  <button 
+    onClick={async () => console.log(await createGame("testing", 300))}>
+    Create Game!
+  </button>
+);
+
 const App = () => {
   return (
     <div className="App">
       <p>TerrAssumptions!</p>
       <hr/>
+      <NewGameButton/>
+      <hr/>
       <InfoComponent/>
     </div>
   );

+ 18 - 1
ui/src/services/ggsh.service.js

@@ -10,4 +10,21 @@ export const getStatus = async () => {
     } catch (err) {
         return {status: err.message, version: null}
     }
-}
+}
+
+export const createGame = async (name, timer) => {
+    const res = await fetch(API_BASE + "/game", {
+        method: "PUT",
+        headers: {
+            "Authorization": `Name ${name}`,
+            "Content-Type": "application/json",
+        },
+        body: JSON.stringify({ timer }),
+    });
+    if (!res.ok) {
+        throw Error(res.statusText);
+    }
+    const { gameId } = await res.json();
+    return gameId;
+}
+