dbHelpers.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {dbUrl} from "./../config/variables";
  2. export const fetchPathKeys = () => {
  3. return fetch(dbUrl + "/get-path")
  4. .then(res => {
  5. if(res.status >= 400) {
  6. throw new Error("Server responds with error!");
  7. }
  8. return res.json();
  9. }).then(data => {
  10. return Object.keys(data)
  11. }, err => {
  12. console.log(err)
  13. });
  14. }
  15. export const fetchPaths = () => {
  16. return fetch(dbUrl + "/get-path")
  17. .then(res => {
  18. if(res.status >= 400) {
  19. throw new Error("Server responds with error!");
  20. }
  21. return res.json();
  22. }).then(data => {
  23. return data
  24. }, err => {
  25. console.log(err)
  26. })
  27. }
  28. export function savePathToDb(){
  29. let path = this.props.path
  30. if ("switchLatLng" in this.props){
  31. path = path.map(pos => [pos[1], pos[0]])
  32. }
  33. let json = JSON.stringify({
  34. path: {
  35. name: this.state.savePathKey,
  36. coords: path
  37. }
  38. })
  39. let reqOptions = {
  40. method: 'POST',
  41. headers: {
  42. 'Accept': 'application/json',
  43. 'Content-Type': 'application/json',
  44. },
  45. body: json
  46. }
  47. return fetch(dbUrl + "/set-path", reqOptions)
  48. .then(res => {
  49. if(res.status >= 400) {
  50. throw new Error("Server responds with error!");
  51. }
  52. return res.text();
  53. }, err => {
  54. console.log(err)
  55. })
  56. }
  57. export function saveTimeToDb(data){
  58. let json = JSON.stringify(data)
  59. let reqOptions = {
  60. method: 'POST',
  61. headers: {
  62. 'Accept': 'application/json',
  63. 'Content-Type': 'application/json',
  64. },
  65. body: json
  66. }
  67. return fetch(dbUrl + "/set-time", reqOptions)
  68. .then(res => {
  69. if(res.status >= 400) {
  70. console.log(res.text)
  71. throw new Error("Server responds with error!");
  72. }
  73. return res.text();
  74. }, err => {
  75. console.log(err)
  76. })
  77. }