App.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { Component } from 'react';
  2. import './App.css';
  3. import Tables from './Tables';
  4. class App extends Component {
  5. constructor(props){
  6. super(props);
  7. this.apiUrl = "http://192.168.17.94:9876";
  8. this.name = null;
  9. this.state = {
  10. persons: []
  11. };
  12. this.componentDidMount = this.componentDidMount.bind(this)
  13. }
  14. // is called once when loading the page
  15. componentDidMount() {
  16. this.reloadLocations();
  17. setInterval(this.updateLocation, 10000);
  18. }
  19. reloadLocations = () => {
  20. fetch(this.apiUrl + "/users")
  21. .then(response => response.json())
  22. .then(data =>{
  23. console.log(data["users"]);
  24. this.setState({ persons: data["users"] })
  25. }).catch(error => {
  26. console.log(error);
  27. });
  28. }
  29. receivedPosition = (position) => {
  30. const axios = require('axios');
  31. axios.put(this.apiUrl + "/report-location", {
  32. "longitude": position.coords.longitude + Math.random()*10,
  33. "latitude": position.coords.latitude,
  34. "timestamp": position.timestamp/1000, // in seconds
  35. "name": this.name
  36. }).then(this.reloadLocations)
  37. .catch(error => {
  38. console.log(error);
  39. });
  40. }
  41. updateLocation = () => {
  42. if (this.name!=null && navigator.geolocation) { //check if geolocation is available
  43. navigator.geolocation.getCurrentPosition(this.receivedPosition);
  44. }
  45. }
  46. render (){
  47. return (
  48. <div id="wrapper">
  49. <Tables persons={this.state.persons}/>
  50. <button
  51. onClick={ () => {
  52. this.name = "Bernie";
  53. }}> Bernie </button>
  54. <button
  55. onClick={ () => {
  56. this.name = "Martin";
  57. }}> Martin </button>
  58. <button
  59. onClick={ () => {
  60. this.name = "Simon";
  61. }}> Simon </button>
  62. <button
  63. onClick={ () => {
  64. this.name = "Lukas";
  65. }}> Lukas </button>
  66. </div>
  67. );
  68. }
  69. }
  70. export default App;