From e74998983cefc5ff5de39c091d07d3f0d9cab2a2 Mon Sep 17 00:00:00 2001
From: Joni Laukka <joni.laukka.overflow@gmail.com>
Date: Thu, 18 Jul 2019 14:02:50 +0300
Subject: [PATCH] Joining updates role state

---
 src/components/GameView.js     | 51 ++++++++++++++++++----------------
 src/components/JoinGameForm.js |  2 +-
 2 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/src/components/GameView.js b/src/components/GameView.js
index f92586e..24c1413 100644
--- a/src/components/GameView.js
+++ b/src/components/GameView.js
@@ -23,30 +23,13 @@ export default class GameView extends React.Component {
 
   componentDidMount() {
     let gameId = new URL(window.location.href).searchParams.get("id");
-    let token = sessionStorage.getItem("token");
-
-    fetch(`${process.env.REACT_APP_API_URL}/game/${gameId}`)
-      .then(res => {
-        if (!res.ok) {
-          throw Error();
-        }
-      })
-      .catch(error => {
-        alert("Game not found");
-        window.document.location.href = "/";
-      });
+    this.getGameInfo(gameId);
+    this.getPlayerRole(gameId);
+  }
 
-    // Get game info
-    fetch(`${process.env.REACT_APP_API_URL}/game/${gameId}`)
-      .then(res => res.json())
-      .then(res => {
-        this.setState({
-          gameInfo: res
-        });
-      })
-      .catch(error => console.log(error));
+  getPlayerRole(gameId) {
+    let token = sessionStorage.getItem("token");
 
-    // Get Role
     fetch(`${process.env.REACT_APP_API_URL}/faction/check-faction/${gameId}`, {
       method: "GET",
       headers: {
@@ -60,6 +43,26 @@ export default class GameView extends React.Component {
       .catch(error => console.log(error));
   }
 
+  getGameInfo(gameId) {
+    fetch(`${process.env.REACT_APP_API_URL}/game/${gameId}`)
+      .then(res => {
+        if (!res.ok) {
+          throw Error();
+        } else {
+          return res.json();
+        }
+      })
+      .then(res => {
+        this.setState({
+          gameInfo: res
+        });
+      })
+      .catch(error => {
+        alert("Game not found");
+        window.document.location.href = "/";
+      });
+  }
+
   handleLeaveFaction = e => {
     let token = sessionStorage.getItem("token");
     let error = false;
@@ -184,7 +187,7 @@ export default class GameView extends React.Component {
                 gameId={this.state.gameInfo.id}
                 toggleView={() => this.setState({ form: "" })}
                 onEditSave={() => {
-                  this.getGameInfo();
+                  this.getGameInfo(this.state.gameInfo.id);
                 }}
               />
             )}
@@ -192,7 +195,7 @@ export default class GameView extends React.Component {
               <JoinGameForm
                 gameId={this.state.gameInfo.id}
                 toggleView={() => this.setState({ form: "" })}
-                onJoin={() => console.log("joinde")}
+                onJoin={() => this.getPlayerRole(this.state.gameInfo.id)}
               />
             )}
             {this.state.form === "players" && (
diff --git a/src/components/JoinGameForm.js b/src/components/JoinGameForm.js
index f4f89ed..6dd3c10 100644
--- a/src/components/JoinGameForm.js
+++ b/src/components/JoinGameForm.js
@@ -14,7 +14,7 @@ export default class JoinGameForm extends React.Component {
   }
 
   // Get game info
-  //TODO: from props
+  //TODO: gameinfo from props
   componentDidMount() {
     if (this.props.gameId === undefined) {
       alert("game not selected");
-- 
GitLab