Skip to content
Snippets Groups Projects
Commit 7a7c5e27 authored by Ronnie Friman's avatar Ronnie Friman
Browse files

reworked fetch functions

parent 2a0e7689
No related branches found
No related tags found
2 merge requests!46Development to testing,!34Filter player replay
...@@ -3,12 +3,10 @@ ...@@ -3,12 +3,10 @@
import React from "react"; import React from "react";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import L from "leaflet"; import L from "leaflet";
import { Map, TileLayer, ZoomControl, Marker, Popup } from "react-leaflet";
import "../track-playback/src/leaflet.trackplayback/clock"; import "../track-playback/src/leaflet.trackplayback/clock";
import "../track-playback/src/leaflet.trackplayback/index"; import "../track-playback/src/leaflet.trackplayback/index";
import "../track-playback/src/control.trackplayback/control.playback"; import "../track-playback/src/control.trackplayback/control.playback";
import "../track-playback/src/control.trackplayback/index"; import "../track-playback/src/control.trackplayback/index";
import DrawGeoJSON from "./DrawGeoJSON";
export default class ReplayMap extends React.Component { export default class ReplayMap extends React.Component {
constructor(props) { constructor(props) {
...@@ -23,30 +21,38 @@ export default class ReplayMap extends React.Component { ...@@ -23,30 +21,38 @@ export default class ReplayMap extends React.Component {
// stores all active drawings on the map // stores all active drawings on the map
activeGeoJSON: [] activeGeoJSON: []
}; };
this.map = React.createRef();
} }
async componentDidMount() { async componentDidMount() {
// set gameId to state from URL
await this.setState({ await this.setState({
gameId: await new URL(window.location.href).searchParams.get("id") gameId: await new URL(window.location.href).searchParams.get("id")
}); });
await this.fetchPlayerData(); // fetch player data with gameId
//await this.fetchDrawingData(); // throws error if game is not found and redirects back to game selection
//this.tickDrawings(); await this.setState({
data: await this.fetchPlayerData()
});
// fetch drawings with gameId
await this.setState({
allGeoJSON: await this.fetchDrawingData()
});
// WIP, map only active drawings to activeGeoJSON state
await this.setState({
activeGeoJSON: this.tickDrawings()
});
// if game was found but game has no replay data, alert the user
this.state.data.length > 1 this.state.data.length > 1
? this.replay() ? this.replay()
: alert("No replay data was found"); : alert("No replay data was found");
} }
componentWillReceiveProps(nextProps) {}
fetchPlayerData = async () => { fetchPlayerData = async () => {
let res = await fetch( let res = await fetch(
`${process.env.REACT_APP_API_URL}/replay/players/${this.state.gameId}` `${process.env.REACT_APP_API_URL}/replay/players/${this.state.gameId}`
); );
if (await res.ok) {
if (res.ok) { return await res.json();
await this.setState({ data: res.json() });
} else { } else {
alert("Game not found"); alert("Game not found");
window.document.location.href = "/"; window.document.location.href = "/";
...@@ -54,30 +60,19 @@ export default class ReplayMap extends React.Component { ...@@ -54,30 +60,19 @@ export default class ReplayMap extends React.Component {
}; };
fetchDrawingData = async () => { fetchDrawingData = async () => {
await fetch(`${process.env.REACT_APP_API_URL}/replay/${this.gameId}`, { let res = await fetch(
method: "GET" `${process.env.REACT_APP_API_URL}/replay/${this.state.gameId}`
}) );
.then(async res => await res.json()) if (await res.ok) {
.then( return await res.json();
async res => { } else {
await this.setState({ allGeoJSON: res }); alert(res.statusText);
}, }
error => {
console.log(error);
}
);
}; };
tickDrawings = () => { tickDrawings = () => {
let activeDrawings = []; return this.state.allGeoJSON.map(drawing => {
this.state.allGeoJSON.map(drawing => { return drawing[0];
activeDrawings.push(drawing[0]);
this.setState({
activeGeoJSON: {
type: "FeatureCollection",
features: [...activeDrawings]
}
});
}); });
}; };
......
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