Skip to content
Snippets Groups Projects
Commit e7499898 authored by Joni Laukka's avatar Joni Laukka
Browse files

Joining updates role state

parent 8e12ebd6
No related branches found
No related tags found
2 merge requests!46Development to testing,!38Join game update fix
......@@ -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" && (
......
......@@ -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");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment