From 2772c3da06848f432fa6f100484b597afe6c2b66 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Taneli=20Riihim=C3=A4ki?= <m3034@student.jamk.fi>
Date: Wed, 19 Jun 2019 14:33:37 +0300
Subject: [PATCH] Got rid of the duplicates, still some known bugs (the map
 clears when drawing something new, and cant delete drawings from the database
 for example)

---
 src/components/DrawTools.js | 10 +++++-----
 src/components/UserMap.js   | 19 +++++++++----------
 2 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/src/components/DrawTools.js b/src/components/DrawTools.js
index 8753129..f7b326a 100644
--- a/src/components/DrawTools.js
+++ b/src/components/DrawTools.js
@@ -19,12 +19,12 @@ class DrawTools extends Component {
   // turn layer to GeoJSON data and add it to an array of all GeoJSON data of the current map
   makeGeoJSON = e => {
     let geoJSON = e.toGeoJSON();
-    let newGeoJSONAll = this.state.geoJSONAll;
-    newGeoJSONAll.push(geoJSON);
-    this.setState({ geoJSONAll: newGeoJSONAll });
+    //let newGeoJSONAll = this.state.geoJSONAll;
+    //newGeoJSONAll.push(geoJSON);
+    //this.setState({ geoJSONAll: newGeoJSONAll });
     console.log(JSON.stringify(geoJSON, null, 4)); // printing GeoJSON data of the previous object create
-    console.log("newGeoJSONAll.length: " + newGeoJSONAll.length);
-    this.props.addToGeojsonLayer(this.state.geoJSONAll);
+    // console.log("newGeoJSONAll.length: " + newGeoJSONAll.length);
+    this.props.addToGeojsonLayer(geoJSON);
   };
 
   render() {
diff --git a/src/components/UserMap.js b/src/components/UserMap.js
index 1340fe5..d777a07 100644
--- a/src/components/UserMap.js
+++ b/src/components/UserMap.js
@@ -35,7 +35,7 @@ class UserMap extends Component {
     });
     this.fetchGeoJSON();
   }
-  // Sends the players marker to the backend
+  // Sends the players drawings to the backend (and database)
   sendGeoJSON() {
     console.log(
       "Lähetettävät jutut: " + JSON.stringify(this.state.geoJSONLayer)
@@ -50,7 +50,7 @@ class UserMap extends Component {
       body: JSON.stringify(this.state.geoJSONLayer)
     });
   }
-  // Mikä vitum readablestream :(
+  // Get the drawings from the backend and add them to the state, so they can be drawn
   fetchGeoJSON() {
     fetch("http://localhost:5000/mapmarkers/getall", {
       method: "GET",
@@ -62,12 +62,10 @@ class UserMap extends Component {
     })
       .then(res => res.json())
       .then(data => {
+        console.log(data);
         let newFeatures = [];
         data.map(item => {
-          item.features[0].map(feature => {
-            newFeatures.push(feature);
-          });
-          return newFeatures;
+          newFeatures.push(item.features);
         });
 
         this.setState({
@@ -132,10 +130,11 @@ class UserMap extends Component {
 
   // Function to be passed to DrawTools so it can add geojson data to this components state
   addToGeojsonLayer(layerToAdd) {
-    let oldFeatures = [];
-    oldFeatures.push(layerToAdd);
-    const newFeatures = oldFeatures;
-    this.setState({ geoJSONLayer: { features: newFeatures } });
+    this.setState({
+      geoJSONLayer: {
+        features: layerToAdd
+      }
+    });
     console.log(
       "Geojsonlayer state: " + JSON.stringify(this.state.geoJSONLayer)
     );
-- 
GitLab