dbHelpers.js 1.8 KB

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