dbHelpers.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. export const fetchPathKeys = () => {
  2. return fetch("http://localhost:5000/get-path")
  3. .then(res => {
  4. if(res.status >= 400) {
  5. throw new Error("Server responds with error!");
  6. }
  7. return res.json();
  8. }).then(data => {
  9. return Object.keys(data)
  10. }, err => {
  11. console.log(err)
  12. });
  13. }
  14. export const fetchPaths = () => {
  15. return fetch("http://localhost:5000/get-path")
  16. .then(res => {
  17. if(res.status >= 400) {
  18. throw new Error("Server responds with error!");
  19. }
  20. return res.json();
  21. }).then(data => {
  22. return data
  23. }, err => {
  24. console.log(err)
  25. })
  26. }
  27. export function savePathToDb(){
  28. let path = this.props.path
  29. if ("switchLatLng" in this.props){
  30. path = path.map(pos => [pos[1], pos[0]])
  31. }
  32. let json = JSON.stringify({
  33. path: {
  34. name: this.state.savePathKey,
  35. coords: path
  36. }
  37. })
  38. let reqOptions = {
  39. method: 'POST',
  40. headers: {
  41. 'Accept': 'application/json',
  42. 'Content-Type': 'application/json',
  43. },
  44. body: json
  45. }
  46. return fetch("http://localhost:5000/set-path", reqOptions)
  47. .then(res => {
  48. if(res.status >= 400) {
  49. throw new Error("Server responds with error!");
  50. }
  51. return res.text();
  52. }, err => {
  53. console.log(err)
  54. })
  55. }