From a9406ccc2a1949827e0027d5eb8f1a7aacda42e2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Taneli=20Riihim=C3=A4ki?= <m3034@student.jamk.fi>
Date: Mon, 24 Jun 2019 14:05:00 +0300
Subject: [PATCH] Fix bugs with layers disappearing when adding new ones, still
 need to work on deleting layers already made

---
 src/components/DrawTools.js |  4 +++-
 src/components/UserMap.js   | 21 +++++++++++++--------
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/components/DrawTools.js b/src/components/DrawTools.js
index f7b326a..352d1df 100644
--- a/src/components/DrawTools.js
+++ b/src/components/DrawTools.js
@@ -22,7 +22,9 @@ class DrawTools extends Component {
     //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(
+      "UserMapille lähetettävä layeri: " + JSON.stringify(geoJSON, null, 4)
+    ); // printing GeoJSON data of the previous object create
     // console.log("newGeoJSONAll.length: " + newGeoJSONAll.length);
     this.props.addToGeojsonLayer(geoJSON);
   };
diff --git a/src/components/UserMap.js b/src/components/UserMap.js
index d777a07..a17894e 100644
--- a/src/components/UserMap.js
+++ b/src/components/UserMap.js
@@ -36,18 +36,21 @@ class UserMap extends Component {
     this.fetchGeoJSON();
   }
   // Sends the players drawings to the backend (and database)
-  sendGeoJSON() {
+  sendGeoJSON(layerToDatabase) {
     console.log(
       "Lähetettävät jutut: " + JSON.stringify(this.state.geoJSONLayer)
     );
-    fetch("http://localhost:5000/mapmarkers/insertLocation", {
+    fetch("http://localhost:5000/mapmarkers/insert-location", {
       method: "PUT",
       headers: {
         Authorization: "Bearer " + sessionStorage.getItem("token"),
         Accept: "application/json",
         "Content-Type": "application/json"
       },
-      body: JSON.stringify(this.state.geoJSONLayer)
+      body: JSON.stringify({
+        type: "FeatureCollection",
+        features: layerToDatabase
+      })
     });
   }
   // Get the drawings from the backend and add them to the state, so they can be drawn
@@ -67,11 +70,12 @@ class UserMap extends Component {
         data.map(item => {
           newFeatures.push(item.features);
         });
+        console.log("Mapataan featureita: ", newFeatures);
 
         this.setState({
           geoJSONLayer: {
             type: "FeatureCollection",
-            features: newFeatures
+            features: [...newFeatures]
           }
         });
         console.log(
@@ -130,15 +134,16 @@ class UserMap extends Component {
 
   // Function to be passed to DrawTools so it can add geojson data to this components state
   addToGeojsonLayer(layerToAdd) {
-    this.setState({
+    this.setState(() => ({
       geoJSONLayer: {
-        features: layerToAdd
+        type: "FeatureCollection",
+        features: [...this.state.geoJSONLayer.features, layerToAdd]
       }
-    });
+    }));
     console.log(
       "Geojsonlayer state: " + JSON.stringify(this.state.geoJSONLayer)
     );
-    this.sendGeoJSON();
+    this.sendGeoJSON(layerToAdd);
   }
 
   render() {
-- 
GitLab