Browse Source

Guess submission endpoint function added

Kirk Trombley 5 years ago
parent
commit
9acf48c8eb
2 changed files with 33 additions and 6 deletions
  1. 12 1
      ui/src/App.js
  2. 21 5
      ui/src/services/ggsh.service.js

+ 12 - 1
ui/src/App.js

@@ -1,5 +1,5 @@
 import React from 'react';
-import { getStatus, createGame, gameInfo, joinGame, getGuesses } from "./services/ggsh.service";
+import { getStatus, createGame, gameInfo, joinGame, getGuesses, sendGuess } from "./services/ggsh.service";
 import './App.css';
 
 class InfoComponent extends React.Component {
@@ -101,6 +101,16 @@ class GameMakerComp extends React.Component {
     this.setState({loading: false, game});
   }
 
+  async handleSendGuess() {
+    this.setState({loading: true});
+    const { gameId, coords } = this.state.game
+    const { currentRound } = await getGuesses(gameId, "testing");
+    const { lat, lng } = coords[currentRound];
+    await sendGuess(gameId, "testing", currentRound, lat, lng);
+    const game = await gameInfo(gameId);
+    this.setState({loading: false, game});
+  }
+
   render() {
     const { loading, game } = this.state;
 
@@ -115,6 +125,7 @@ class GameMakerComp extends React.Component {
           <p>Your Guesses:</p>
           <GuessComp gameId={game.gameId} name="testing"/>
           <button onClick={() => this.handleGameJoin()}>Add second user!</button>
+          <button onClick={() => this.handleSendGuess()}>Send perfect guess!</button>
         </div>
       );
     }

+ 21 - 5
ui/src/services/ggsh.service.js

@@ -2,7 +2,7 @@ import { API_BASE } from "../config";
 
 export const getStatus = async () => {
     try {
-        const res = await fetch(API_BASE + "/");
+        const res = await fetch(API_BASE);
         if (!res.ok) {
             throw Error(res.statusText);
         }
@@ -13,7 +13,7 @@ export const getStatus = async () => {
 }
 
 export const createGame = async (name, timer) => {
-    const res = await fetch(API_BASE + "/game", {
+    const res = await fetch(`${API_BASE}/game`, {
         method: "PUT",
         headers: {
             "Authorization": `Name ${name}`,
@@ -29,7 +29,7 @@ export const createGame = async (name, timer) => {
 }
 
 export const gameInfo = async (gameId) => {
-    const res = await fetch(API_BASE + `/game/${gameId}`);
+    const res = await fetch(`${API_BASE}/game/${gameId}`);
     if (!res.ok) {
         throw Error(res.statusText);
     }
@@ -37,7 +37,7 @@ export const gameInfo = async (gameId) => {
 }
 
 export const joinGame = async (gameId, name) => {
-    const res = await fetch(API_BASE + `/game/${gameId}/join`, {
+    const res = await fetch(`${API_BASE}/game/${gameId}/join`, {
         method: "POST",
         headers: {
             "Authorization": `Name ${name}`
@@ -49,7 +49,7 @@ export const joinGame = async (gameId, name) => {
 }
 
 export const getGuesses = async (gameId, name) => {
-    const res = await fetch(API_BASE + `/game/${gameId}/guesses`, {
+    const res = await fetch(`${API_BASE}/game/${gameId}/guesses`, {
         headers: {
             "Authorization": `Name ${name}`
         },
@@ -59,3 +59,19 @@ export const getGuesses = async (gameId, name) => {
     }
     return await res.json();
 }
+
+export const sendGuess = async (gameId, name, round, lat, lng) => {
+    const res = await fetch(`${API_BASE}/game/${gameId}/guesses/${round}`, {
+        method: "POST",
+        headers: {
+            "Authorization": `Name ${name}`,
+            "Content-Type": "application/json",
+        },
+        body: JSON.stringify({ lat, lng }),
+    });
+    if (!res.ok) {
+        throw Error(res.statusText);
+    }
+    const { score } = await res.json();
+    return score;
+}