diff --git a/src/components/GameView.js b/src/components/GameView.js index 7a2c4b05e29a527705e5920f409abd7d4808538b..a6f19ecd92759fa930d68769bd7a14535d32aba1 100644 --- a/src/components/GameView.js +++ b/src/components/GameView.js @@ -23,8 +23,27 @@ export default class GameView extends React.Component { componentDidMount() { let gameId = new URL(window.location.href).searchParams.get("id"); + this.getGameInfo(gameId); + this.getPlayerRole(gameId); + } + + getPlayerRole(gameId) { let token = sessionStorage.getItem("token"); + fetch(`${process.env.REACT_APP_API_URL}/faction/check-faction/${gameId}`, { + method: "GET", + headers: { + Authorization: "Bearer " + token + } + }) + .then(res => res.json()) + .then(res => { + this.setState({ role: res.role }); + }) + .catch(error => console.log(error)); + } + + getGameInfo(gameId) { fetch(`${process.env.REACT_APP_API_URL}/game/${gameId}`) .then(res => { if (!res.ok) { @@ -42,19 +61,6 @@ export default class GameView extends React.Component { alert("Game not found"); window.document.location.href = "/"; }); - - // Get Role - fetch(`${process.env.REACT_APP_API_URL}/faction/check-faction/${gameId}`, { - method: "GET", - headers: { - Authorization: "Bearer " + token - } - }) - .then(res => res.json()) - .then(res => { - this.setState({ role: res.role }); - }) - .catch(error => console.log(error)); } handleLeaveFaction = e => { @@ -79,6 +85,7 @@ export default class GameView extends React.Component { }) .then(res => { alert(res.message); + this.getPlayerRole(this.state.gameInfo.id); }) .catch(error => console.log(error)); }; @@ -182,7 +189,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); }} /> )} @@ -190,7 +197,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 038bbae9999b5a6a9ab4813d5628a1fbc13e9078..b638b61f2aeb6ac745f9afde6aa833103f69d318 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");