diff --git a/src/components/Player.js b/src/components/Player.js new file mode 100644 index 0000000000000000000000000000000000000000..c64884b6996200de9b039a65b5a53755779115c4 --- /dev/null +++ b/src/components/Player.js @@ -0,0 +1,85 @@ +import React, { Component } from "react"; +import { Marker, Popup } from "react-leaflet"; + +class Player extends Component { + constructor(props) { + super(props); + this.state = { + players: null + }; + } + + getPlayers() { + fetch( + "http://localhost:5000/tracking/players/" + this.props.currentGameId, + { + method: "GET", + headers: { + Authorization: "Bearer " + sessionStorage.getItem("token") + } + } + ) + .then(res => res.json()) // no brackets over res.json() or it breaks (what) + .then(data => { + // don't do anything if data is not an array, as it breaks the map function at render + if (Array.isArray(data)) { + this.setState({ + players: data + }); + } + }) + .catch(error => { + console.log(error); + }); + } + + shouldComponentUpdate(nextProps, nextState) { + // do not update component until players have been fetched and game ID is available + if (this.state.players === null) { + this.getPlayers(); + return false; + } else if (this.props.currentGameId === null) { + return false; + } else { + return true; + } + } + + componentDidUpdate() { + // check if game ID has changed + if (this.state.currentGameId !== this.props.currentGameId) { + this.setState({ + currentGameId: this.props.currentGameId + }); + } + } + + render() { + return ( + <div> + {this.state.players !== null && + this.state.players.map(player => { + return ( + <Marker + key={Math.random()} + position={player.coordinates} + factionId={player.factionId} + gamepersonId={player.gamepersonId} + gamepersonRole={player.gamepersonRole} + > + <Popup> + <b>factionId:</b> {player.factionId} + <br /> + <b>gamepersonId:</b> {player.gamepersonId} + <br /> + <b>gamepersonRole:</b> {player.gamepersonRole} + </Popup> + </Marker> + ); + })} + </div> + ); + } +} + +export default Player; diff --git a/src/components/UserMap.js b/src/components/UserMap.js index ad20ffd4e6215faf4bb33610ac65e3b6a9460680..22dbbb66cc08c872820a108643cfc4e9e1fe6b73 100644 --- a/src/components/UserMap.js +++ b/src/components/UserMap.js @@ -1,6 +1,7 @@ import React, { Component } from "react"; import { Map, TileLayer, ZoomControl, Marker, Popup } from "react-leaflet"; import DrawTools from "./DrawTools.js"; +import Player from "./Player.js"; class UserMap extends Component { constructor(props) { @@ -111,7 +112,6 @@ class UserMap extends Component { .catch(error => { console.log(error); }); - console.log(this.state.geoJSONLayer); } componentWillUnmount() { @@ -190,6 +190,7 @@ class UserMap extends Component { </Popup> </Marker> )} + <Player currentGameId={this.state.currentGameId} /> </Map> ); }