From 7895ffd89a44f35531b7a7c6cc0f7bfd73627d6f Mon Sep 17 00:00:00 2001
From: Joni Laukka <joni.laukka.overflow@gmail.com>
Date: Fri, 12 Jul 2019 13:11:54 +0300
Subject: [PATCH] Gamelist updates if the list changes (add, edit, delete)

---
 src/components/EditGameForm.js  | 4 ++++
 src/components/GameCard.js      | 8 ++++++++
 src/components/GameList.js      | 2 +-
 src/components/GameSelection.js | 7 ++++++-
 4 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/components/EditGameForm.js b/src/components/EditGameForm.js
index 55247e6..a72a05a 100644
--- a/src/components/EditGameForm.js
+++ b/src/components/EditGameForm.js
@@ -177,6 +177,7 @@ export class EditGameForm extends React.Component {
         .then(result => {
           console.log(result);
           this.handleView();
+          this.props.onEditSave();
         })
         .catch(error => console.log(error));
     }
@@ -267,6 +268,7 @@ export class EditGameForm extends React.Component {
       })
       .then(result => {
         alert(result.message);
+        this.props.onEditSave();
         this.handleView();
       })
       .catch(error => console.log("Error: ", error));
@@ -305,6 +307,7 @@ export class EditGameForm extends React.Component {
       .then(result => {
         let factions = result.map(faction => {
           return {
+            factionId: faction.factionId,
             factionName: faction.factionName,
             factionPassword: faction.factionPassword,
             multiplier: 1,
@@ -315,6 +318,7 @@ export class EditGameForm extends React.Component {
         // Remove objective point's id from the object
         let objectivePoints = json.objective_points.map(point => {
           return {
+            objectivePointId: point.objectivePointId,
             objectivePointDescription: point.objectivePointDescription,
             objectivePointMultiplier: point.objectivePointMultiplier
           };
diff --git a/src/components/GameCard.js b/src/components/GameCard.js
index 6cb6516..bb54f1a 100644
--- a/src/components/GameCard.js
+++ b/src/components/GameCard.js
@@ -10,6 +10,10 @@ export default class GameCard extends React.Component {
 
   // Get game info
   componentDidMount() {
+    this.getGameInfo();
+  }
+
+  getGameInfo() {
     fetch(`${process.env.REACT_APP_API_URL}/game/${this.props.gameId}`)
       .then(res => {
         if (res.ok) {
@@ -63,6 +67,10 @@ export default class GameCard extends React.Component {
           <EditGameForm
             gameId={this.state.gameInfo.id}
             toggleView={() => this.setState({ editForm: false })}
+            onEditSave={() => {
+              this.props.onEditSave();
+              this.getGameInfo();
+            }}
           />
         )}
       </div>
diff --git a/src/components/GameList.js b/src/components/GameList.js
index f756ade..7427988 100644
--- a/src/components/GameList.js
+++ b/src/components/GameList.js
@@ -8,7 +8,7 @@ class GameList extends React.Component {
         <GameCard
           key={game.id}
           gameId={game.id}
-          onEditSave={() => this.getGames()}
+          onEditSave={this.props.onEditSave}
         />
       );
     });
diff --git a/src/components/GameSelection.js b/src/components/GameSelection.js
index db948cd..9759aec 100644
--- a/src/components/GameSelection.js
+++ b/src/components/GameSelection.js
@@ -49,7 +49,12 @@ export default class GameSelection extends React.Component {
             }
           />
         )}
-        <GameList games={this.state.games} />
+        <GameList
+          games={this.state.games}
+          onEditSave={() => {
+            this.getGames();
+          }}
+        />
       </div>
     );
   }
-- 
GitLab