|
@@ -1,6 +1,4 @@
|
|
import React, { Component } from 'react';
|
|
import React, { Component } from 'react';
|
|
-//import persons from './persons';
|
|
|
|
-//import ReactDOM from 'react-dom';
|
|
|
|
import './App.css';
|
|
import './App.css';
|
|
import Tables from './Tables';
|
|
import Tables from './Tables';
|
|
|
|
|
|
@@ -8,6 +6,8 @@ import Tables from './Tables';
|
|
class App extends Component {
|
|
class App extends Component {
|
|
constructor(props){
|
|
constructor(props){
|
|
super(props);
|
|
super(props);
|
|
|
|
+ this.apiUrl = "http://localhost:5000";
|
|
|
|
+ this.name = null;
|
|
this.state = {
|
|
this.state = {
|
|
persons: []
|
|
persons: []
|
|
};
|
|
};
|
|
@@ -17,140 +17,62 @@ class App extends Component {
|
|
|
|
|
|
// is called once when loading the page
|
|
// is called once when loading the page
|
|
componentDidMount() {
|
|
componentDidMount() {
|
|
- //fetch("http://192.168.8.100:3001/persons")
|
|
|
|
- fetch("http://localhost:5000/users")
|
|
|
|
- .then(response => {
|
|
|
|
- if (response.ok) {
|
|
|
|
- return response;
|
|
|
|
- } else {
|
|
|
|
- let errorMessage = "${response.status(${response.statusText})",
|
|
|
|
- error = new Error(errorMessage);
|
|
|
|
- throw(error);
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
|
|
+ this.reloadLocations();
|
|
|
|
+ setInterval(this.updateLocation, 10000);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ reloadLocations = () => {
|
|
|
|
+ fetch(this.apiUrl + "/users")
|
|
.then(response => response.json())
|
|
.then(response => response.json())
|
|
- .then(json =>{
|
|
|
|
- console.log(json["users"]);
|
|
|
|
- this.setState({ persons: json["users"] })
|
|
|
|
|
|
+ .then(data =>{
|
|
|
|
+ console.log(data["users"]);
|
|
|
|
+ this.setState({ persons: data["users"] })
|
|
}).catch(error => {
|
|
}).catch(error => {
|
|
console.log(error);
|
|
console.log(error);
|
|
});
|
|
});
|
|
-
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- /*change = () => {
|
|
|
|
- const newPersons = [...this.state.persons];
|
|
|
|
- const id = 0;
|
|
|
|
- newPersons.map( person => {
|
|
|
|
- if (id == person.id) {
|
|
|
|
- const axios = require('axios');
|
|
|
|
- let longitude = Math.floor((Math.random() * 150) + 1);
|
|
|
|
- let latitude = Math.floor((Math.random() * 150) + 1);
|
|
|
|
-
|
|
|
|
- //axios.put('http://192.168.8.100:3001/persons/0/', {
|
|
|
|
- axios.put('http://localhost:3001/persons/0/', {
|
|
|
|
- longitude: longitude,
|
|
|
|
- latitude: latitude,
|
|
|
|
- shortName: "VEN",
|
|
|
|
- name: "Martin",
|
|
|
|
- speed: 1,
|
|
|
|
- color: "#ddd"
|
|
|
|
- }).catch(error => {
|
|
|
|
- console.log(error);
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- })
|
|
|
|
- this.componentDidMount()
|
|
|
|
-
|
|
|
|
- }*/
|
|
|
|
|
|
|
|
- getLocation = (name) => {
|
|
|
|
|
|
+ receivedPosition = (position) => {
|
|
const axios = require('axios');
|
|
const axios = require('axios');
|
|
- const newPersons = [...this.state.persons];
|
|
|
|
-
|
|
|
|
- if (navigator.geolocation) { //check if geolocation is available
|
|
|
|
- navigator.geolocation.getCurrentPosition(function(position){
|
|
|
|
- console.log(position);
|
|
|
|
- console.log({"longitude": position.coords.longitude, "latitude": position.coords.latitude, "timestamp": position.timestamp, "name": name})
|
|
|
|
-
|
|
|
|
- axios.put('http://localhost:5000/report-location', {
|
|
|
|
- "longitude": position.coords.longitude,
|
|
|
|
- "latitude": position.coords.latitude,
|
|
|
|
- "timestamp": position.timestamp,
|
|
|
|
- "name": name}).catch(error => {
|
|
|
|
- console.log(error);
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- newPersons.map( person => {
|
|
|
|
- if (name == person.name) {
|
|
|
|
- person.longitude = position.coords.longitude
|
|
|
|
- person.latitude = position.coords.latitude
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- this.setState({ persons: newPersons });
|
|
|
|
|
|
+ axios.put(this.apiUrl + "/report-location", {
|
|
|
|
+ "longitude": position.coords.longitude + Math.random()*10,
|
|
|
|
+ "latitude": position.coords.latitude,
|
|
|
|
+ "timestamp": position.timestamp/1000, // in seconds
|
|
|
|
+ "name": this.name
|
|
|
|
+ }).then(this.reloadLocations)
|
|
|
|
+ .catch(error => {
|
|
|
|
+ console.log(error);
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ updateLocation = () => {
|
|
|
|
+ if (this.name!=null && navigator.geolocation) { //check if geolocation is available
|
|
|
|
+ navigator.geolocation.getCurrentPosition(this.receivedPosition);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
- /*create = (id) => {
|
|
|
|
- const axios = require('axios');
|
|
|
|
- let longitude = Math.floor((Math.random() * 100) + 1);
|
|
|
|
- let latitude = Math.floor((Math.random() * 100) + 1);
|
|
|
|
-
|
|
|
|
- if (navigator.geolocation) { //check if geolocation is available
|
|
|
|
- navigator.geolocation.getCurrentPosition(
|
|
|
|
- function(position){
|
|
|
|
- console.log(position);
|
|
|
|
- longitude = position.coords.longitude
|
|
|
|
- latitude = position.coords.latitude
|
|
|
|
- })
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- //axios.put('http://localhost:3001/persons/0/', {
|
|
|
|
- axios.put('http://localhost:3001/persons/0/', {
|
|
|
|
- longitude: longitude,
|
|
|
|
- latitude: latitude,
|
|
|
|
- shortName: "VEN",
|
|
|
|
- name: "Martin", // *** getAttribute
|
|
|
|
- speed: 1,
|
|
|
|
- color: "#ddd"
|
|
|
|
- }).catch(error => {
|
|
|
|
- console.log(error);
|
|
|
|
- });
|
|
|
|
- }*/
|
|
|
|
-
|
|
|
|
render (){
|
|
render (){
|
|
- //setTimeout(this.componentDidMount, 10000)
|
|
|
|
-
|
|
|
|
return (
|
|
return (
|
|
<div id="wrapper">
|
|
<div id="wrapper">
|
|
<Tables persons={this.state.persons}/>
|
|
<Tables persons={this.state.persons}/>
|
|
- <div id="debug"> </div>
|
|
|
|
|
|
+
|
|
<button
|
|
<button
|
|
onClick={ () => {
|
|
onClick={ () => {
|
|
- this.getLocation("Bernie");
|
|
|
|
- }}
|
|
|
|
- > Bernie </button>
|
|
|
|
|
|
+ this.name = "Bernie";
|
|
|
|
+ }}> Bernie </button>
|
|
<button
|
|
<button
|
|
onClick={ () => {
|
|
onClick={ () => {
|
|
- this.getLocation("Martin");
|
|
|
|
- }}
|
|
|
|
- > Martin </button>
|
|
|
|
|
|
+ this.name = "Martin";
|
|
|
|
+ }}> Martin </button>
|
|
<button
|
|
<button
|
|
onClick={ () => {
|
|
onClick={ () => {
|
|
- this.getLocation("Simon");
|
|
|
|
- }}
|
|
|
|
- > Simon </button>
|
|
|
|
|
|
+ this.name = "Simon";
|
|
|
|
+ }}> Simon </button>
|
|
<button
|
|
<button
|
|
onClick={ () => {
|
|
onClick={ () => {
|
|
- this.getLocation("Lukas");
|
|
|
|
- }}
|
|
|
|
- > Lukas </button>
|
|
|
|
|
|
+ this.name = "Lukas";
|
|
|
|
+ }}> Lukas </button>
|
|
</div>
|
|
</div>
|
|
);
|
|
);
|
|
}
|
|
}
|