diff --git a/src/components/EditGameForm.js b/src/components/EditGameForm.js index da85c6dd00d93a634bebb935d5eee31748260352..5e0f9b2390a3a46de1fbd1c1fb48a36b3bbe9426 100644 --- a/src/components/EditGameForm.js +++ b/src/components/EditGameForm.js @@ -30,7 +30,8 @@ export default class EditGameForm extends React.Component { objectivePoints: [], capture_time: 300, confirmation_time: 60, - displayColorPicker: false + displayColorPicker: false, + saved: false }; this.handleMapDrag = this.handleMapDrag.bind(this); @@ -185,7 +186,12 @@ export default class EditGameForm extends React.Component { // show/hide this form handleView = e => { - this.props.toggleView(this.props.view); + if ( + this.state.saved || + window.confirm("Are you sure you want to leave without saving?") + ) { + this.props.toggleView(this.props.view); + } }; // remove view with ESC @@ -248,6 +254,7 @@ export default class EditGameForm extends React.Component { } let token = sessionStorage.getItem("token"); + let error = false; // Send Game info to the server fetch(`${process.env.REACT_APP_API_URL}/game/edit/${this.props.gameId}`, { @@ -261,15 +268,23 @@ export default class EditGameForm extends React.Component { }) .then(res => { if (!res.ok) { - throw Error(res.statusMessage); - } else { - return res.json(); + error = true; } + return res.json(); }) .then(result => { alert(result.message); - this.props.onEditSave(); - this.handleView(); + if (!error) { + this.setState( + { + saved: true + }, + () => { + this.handleView(); + this.props.onEditSave(); + } + ); + } }) .catch(error => console.log("Error: ", error)); }; diff --git a/src/components/GameStateButtons.js b/src/components/GameStateButtons.js index c770ed7896facdddaf56adecde4c689d10dbeb3f..49d1f2badf64a310d49af5c17a747697293bc1cf 100644 --- a/src/components/GameStateButtons.js +++ b/src/components/GameStateButtons.js @@ -6,39 +6,42 @@ export default class GameStateButtons extends React.Component { }; setGameState(state) { - console.log(state); - let token = sessionStorage.getItem("token"); - let error = false; - fetch( - `${process.env.REACT_APP_API_URL}/game/edit-state/${this.props.gameId}`, - { - method: "PUT", - headers: { - Authorization: "Bearer " + token, - Accept: "application/json", - "Content-Type": "application/json" - }, - body: JSON.stringify({ - id: this.props.gameId, - state: state - }) - } - ) - .then(res => { - if (!res.ok) { - error = true; - } - return res.json(); - }) - .then(res => { - if (error) { - console.log(res); - } else { - alert(`Game state changed to ${state}`); - this.setState({ gameState: state }); + if ( + window.confirm(`Are you sure you want to change game state to ${state}?`) + ) { + let token = sessionStorage.getItem("token"); + let error = false; + fetch( + `${process.env.REACT_APP_API_URL}/game/edit-state/${this.props.gameId}`, + { + method: "PUT", + headers: { + Authorization: "Bearer " + token, + Accept: "application/json", + "Content-Type": "application/json" + }, + body: JSON.stringify({ + id: this.props.gameId, + state: state + }) } - }) - .catch(error => console.log(error)); + ) + .then(res => { + if (!res.ok) { + error = true; + } + return res.json(); + }) + .then(res => { + if (error) { + console.log(res); + } else { + alert(`Game state changed to ${state}`); + this.setState({ gameState: state }); + } + }) + .catch(error => console.log(error)); + } } render() { diff --git a/src/components/NotificationView.js b/src/components/NotificationView.js index d7538066f6ac3058ec27c6cd2716383de6eed215..d341808782a0211f46bed8ff240993783d3127bb 100644 --- a/src/components/NotificationView.js +++ b/src/components/NotificationView.js @@ -30,12 +30,15 @@ export default class NotificationView extends React.Component { if (this.state.notificationInput === "") { alert("notification message can't be empty"); - } else { + } else if ( + window.confirm("Are you sure you want to send the notification") + ) { this.props.socket.emit("game-info", { type: this.state.notificationTypeInput, message: this.state.notificationInput, game: this.props.gameId }); + alert("Notification sent"); this.getNotifications(this.props.gameId); this.setState({ notificationInput: "" }); }