Skip to content
Snippets Groups Projects

Notifications + small updates

Merged H9031 requested to merge notification-view into development
9 files
+ 147
77
Compare changes
  • Side-by-side
  • Inline
Files
9
+ 86
22
@@ -8,6 +8,8 @@ import PlayerlistView from "./PlayerlistView";
@@ -8,6 +8,8 @@ import PlayerlistView from "./PlayerlistView";
import NotificationView from "./NotificationView";
import NotificationView from "./NotificationView";
import GameStateButtons from "./GameStateButtons";
import GameStateButtons from "./GameStateButtons";
import ClientSocket from "./Socket";
import ClientSocket from "./Socket";
 
import NotificationPopup from "./NotificationPopup";
 
import GameInfoView from "./GameInfoView";
export default class GameView extends React.Component {
export default class GameView extends React.Component {
state = {
state = {
@@ -18,7 +20,8 @@ export default class GameView extends React.Component {
@@ -18,7 +20,8 @@ export default class GameView extends React.Component {
lng: 25.7597186,
lng: 25.7597186,
zoom: 13,
zoom: 13,
mapUrl: "https://tiles.kartat.kapsi.fi/taustakartta/{z}/{x}/{y}.jpg",
mapUrl: "https://tiles.kartat.kapsi.fi/taustakartta/{z}/{x}/{y}.jpg",
socketSignal: null
socketSignal: null,
 
socket: null
};
};
componentDidMount() {
componentDidMount() {
@@ -79,24 +82,54 @@ export default class GameView extends React.Component {
@@ -79,24 +82,54 @@ export default class GameView extends React.Component {
)
)
.then(res => {
.then(res => {
if (!res.ok) {
if (!res.ok) {
error = true;
throw Error();
 
} else {
 
return res.json();
}
}
return res.json();
})
})
.then(res => {
.then(res => {
alert(res.message);
alert(res.message);
this.getPlayerRole(this.state.gameInfo.id);
this.getPlayerRole(this.state.gameInfo.id);
})
})
.catch(error => console.log(error));
.catch(error => {
 
alert("Game not found");
 
window.document.location.href = "/";
 
});
 
};
 
 
handleLeaveFaction = e => {
 
if (window.confirm("Are you sure you want to leave your faction?")) {
 
let token = sessionStorage.getItem("token");
 
fetch(
 
`${process.env.REACT_APP_API_URL}/faction/leave/${
 
this.state.gameInfo.id
 
}`,
 
{
 
method: "DELETE",
 
headers: {
 
Authorization: "Bearer " + token
 
}
 
}
 
)
 
.then(res => {
 
if (!res.ok) {
 
}
 
return res.json();
 
})
 
.then(res => {
 
alert(res.message);
 
this.getPlayerRole(this.state.gameInfo.id);
 
})
 
.catch(error => console.log(error));
 
}
};
};
// setting the socket signal automatically fires shouldComponentUpdate function where socketSignal prop is present
// setting the socket signal automatically fires shouldComponentUpdate function where socketSignal prop is present
// setting socketSignal to null immediately after to avoid multiple database fetches
// setting socketSignal to null immediately after to avoid multiple database fetches
getSocketSignal = type => {
getSocketSignal = data => {
console.log(type);
this.setState(
this.setState(
{
{
socketSignal: type
socketSignal: data
},
},
() => {
() => {
this.setState({
this.setState({
@@ -106,13 +139,18 @@ export default class GameView extends React.Component {
@@ -106,13 +139,18 @@ export default class GameView extends React.Component {
);
);
};
};
 
onSocketChange = newSocket => {
 
this.setState({
 
socket: newSocket
 
});
 
};
 
render() {
render() {
const initialPosition = [this.state.lat, this.state.lng];
const initialPosition = [this.state.lat, this.state.lng];
return (
return (
<div>
<div>
<Link to="/">
<Link to="/">
<button>Game selection</button>
<button id="gameViewGameSelectionButton">Game selection</button>
</Link>
</Link>
{this.state.gameInfo !== null && (
{this.state.gameInfo !== null && (
<div>
<div>
@@ -120,6 +158,7 @@ export default class GameView extends React.Component {
@@ -120,6 +158,7 @@ export default class GameView extends React.Component {
<ClientSocket
<ClientSocket
gameId={this.state.gameInfo.id}
gameId={this.state.gameInfo.id}
getSocketSignal={this.getSocketSignal}
getSocketSignal={this.getSocketSignal}
 
onSocketChange={this.onSocketChange}
/>
/>
)}
)}
<div>Game Name: {this.state.gameInfo.name}</div>
<div>Game Name: {this.state.gameInfo.name}</div>
@@ -129,14 +168,21 @@ export default class GameView extends React.Component {
@@ -129,14 +168,21 @@ export default class GameView extends React.Component {
{this.state.role !== "" && (
{this.state.role !== "" && (
<div>Your role in this game: {this.state.role}</div>
<div>Your role in this game: {this.state.role}</div>
)}
)}
{this.state.role === "admin" && (
{this.state.role === "admin" &&
<button
this.state.gameInfo.state === "CREATED" && (
id="editGameButton"
<button
onClick={() => this.setState({ form: "edit" })}
id="editGameButton"
>
onClick={() => this.setState({ form: "edit" })}
Edit
>
</button>
Edit
)}
</button>
 
)}
 
<button
 
id="gameInfoButton"
 
onClick={() => this.setState({ form: "info" })}
 
>
 
Game Info
 
</button>
{this.state.role === "" && (
{this.state.role === "" && (
<button
<button
id="joinGameButton"
id="joinGameButton"
@@ -163,6 +209,7 @@ export default class GameView extends React.Component {
@@ -163,6 +209,7 @@ export default class GameView extends React.Component {
<TaskListButton
<TaskListButton
gameId={this.state.gameInfo.id}
gameId={this.state.gameInfo.id}
role={this.state.role}
role={this.state.role}
 
factions={this.state.gameInfo.factions}
/>
/>
)}
)}
{this.state.role !== "admin" && this.state.role !== "" && (
{this.state.role !== "admin" && this.state.role !== "" && (
@@ -181,16 +228,20 @@ export default class GameView extends React.Component {
@@ -181,16 +228,20 @@ export default class GameView extends React.Component {
zoom={this.state.zoom}
zoom={this.state.zoom}
mapUrl={this.state.mapUrl}
mapUrl={this.state.mapUrl}
currentGameId={this.state.gameInfo.id}
currentGameId={this.state.gameInfo.id}
socketSignal={this.state.socketSignal}
socketSignal={
 
this.state.socketSignal === null
 
? null
 
: this.state.socketSignal.type
 
}
role={this.state.role}
role={this.state.role}
/>
>
 
<NotificationPopup socketSignal={this.state.socketSignal} />
 
</UserMap>
{this.state.form === "edit" && (
{this.state.form === "edit" && (
<EditGameForm
<EditGameForm
gameId={this.state.gameInfo.id}
gameId={this.state.gameInfo.id}
toggleView={() => this.setState({ form: "" })}
toggleView={() => this.setState({ form: "" })}
onEditSave={() => {
onEditSave={() => this.getGameInfo(this.state.gameInfo.id)}
this.getGameInfo(this.state.gameInfo.id);
}}
/>
/>
)}
)}
{this.state.form === "join" && (
{this.state.form === "join" && (
@@ -211,6 +262,19 @@ export default class GameView extends React.Component {
@@ -211,6 +262,19 @@ export default class GameView extends React.Component {
<NotificationView
<NotificationView
gameId={this.state.gameInfo.id}
gameId={this.state.gameInfo.id}
toggleView={() => this.setState({ form: "" })}
toggleView={() => this.setState({ form: "" })}
 
socket={this.state.socket}
 
role={this.state.role}
 
gameState={
 
this.state.gameInfo !== undefined
 
? this.state.gameInfo.state
 
: ""
 
}
 
/>
 
)}
 
{this.state.form === "info" && (
 
<GameInfoView
 
gameInfo={this.state.gameInfo}
 
toggleView={() => this.setState({ form: "" })}
/>
/>
)}
)}
</div>
</div>
Loading