diff --git a/src/components/GameView.js b/src/components/GameView.js index f92586e94bc5b379b47914ab6bf0a3954fe7c28e..24c1413aed0343d984f8df5b58c7c0fcd96d88f9 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 f4f89edcacf82bbe4281dc5b419f694de0689eb6..6dd3c10b14a47639cb1db720b3e698001b122065 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");