dbHelpers.js 1.2 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 json = JSON.stringify({
  29. "path": {
  30. "name": this.state.savePathKey,
  31. "coords": this.state.path
  32. }
  33. })
  34. let reqOptions = {
  35. method: 'POST',
  36. headers: {
  37. 'Accept': 'application/json',
  38. 'Content-Type': 'application/json',
  39. },
  40. body: json
  41. }
  42. return fetch("http://localhost:5000/set-path", reqOptions)
  43. .then(res => {
  44. if(res.status >= 400) {
  45. throw new Error("Server responds with error!");
  46. }
  47. return res.json();
  48. })
  49. .then(data => {
  50. return data
  51. }, err => {
  52. console.log(err)
  53. })
  54. }