Bernadette Elena Hammerle 4 жил өмнө
parent
commit
b9d977a4f3

+ 16 - 1
src/Leaflet.js

@@ -28,13 +28,27 @@ export default class Leaflet extends React.Component{
         }
         return res.json();
       }).then(data => {
-        this.setState({path: data.path})
+        this.setState({path: data})
         this.getDistance()
       }, err => {
         console.log(err)
       });
   }
 
+  setPath = () => {
+    let json = JSON.stringify({
+        "path": this.state.path
+      })
+    fetch("http://localhost:5000/set-path", {
+      method: 'POST',
+      headers: {
+        'Accept': 'application/json',
+        'Content-Type': 'application/json',
+      },
+      body: json
+    })
+  }
+
   newPathPoint = event => {
     const { lat, lng } = event.latlng
     console.log("Clicked at", lat, "/", lng)
@@ -101,6 +115,7 @@ export default class Leaflet extends React.Component{
           <button onClick={this.clearPath}>Clear path</button>
           <button onClick={this.getDistance}>Calculate Distance</button>
           <button onClick={this.fetchPath}>fetch path</button>
+          <button onClick={this.setPath}>save path</button>
           <p>
             Distanz: {Math.round(this.state.distance*100)/100} km
           </p>

+ 13 - 4
src/databaseInterface.py

@@ -5,12 +5,21 @@ import json
 app = Flask(__name__)
 CORS(app)
 
+
 @app.route("/get-path", methods=["GET"])
 def get_path():
-    with open("path.json") as pathFile:
-        pathStr = pathFile.read()
-        path = json.loads(pathStr)
-    return path, 200
+    with open("path.json", "r") as pathFile:
+        path = pathFile.read()
+    return path
+
+
+@app.route("/set-path", methods=["POST"])
+def set_path():
+    path = request.json.get("path")
+    with open("path.json", "w") as pathFile:
+        json.dump(path, pathFile)
+    return "ok", 200
+
 
 if __name__=="__main__":
     app.run(host='0.0.0.0')