api_calls.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //static/js/api_calls.js
  2. let fake_local_session = { username: 'nobody' };
  3. let fake_local_votesdata = [
  4. {
  5. date: "2024-04-01",
  6. votes: {
  7. 'who': "yes",
  8. 'what': "no",
  9. },
  10. },
  11. {
  12. date: "2024-04-02",
  13. votes: {
  14. 'what': "no",
  15. 'when': "yes",
  16. 'where': "yes",
  17. },
  18. },
  19. {
  20. date: "2024-04-03",
  21. votes: {
  22. 'how': "no",
  23. 'random': Math.random() < 0.5 ? "no" : "yes",
  24. },
  25. },
  26. ]
  27. ////// This line, and everything above this line, should be deleted when you get the AJAX working. Really.
  28. async function getSessionFromBackend() {
  29. try {
  30. const response = await axios.get('/api/v1/getSession');
  31. return response.data;
  32. } catch (error) {
  33. console.error('Error fetching session from backend:', error);
  34. return { success: false, data: null, error: error.toString() };
  35. }
  36. }
  37. async function getVotesFromBackend() {
  38. try {
  39. const response = await axios.get('/api/v1/votes/list');
  40. return response.data;
  41. } catch (error) {
  42. console.error('Error fetching votes from backend:', error);
  43. return { success: false, data: null, error: error.toString() };
  44. }
  45. }
  46. async function setMyVote(username, date, vote) {
  47. try {
  48. const response = await axios.post('/api/v1/votes/set', { username, date, vote });
  49. return response.data;
  50. } catch (error) {
  51. console.error('Error setting vote:', error.response ? error.response.data.error : error);
  52. return { success: false, error: error.response ? error.response.data.error : 'Unknown error' };
  53. }
  54. }
  55. async function ajaxSignup(username, password) {
  56. try {
  57. const response = await axios.post('/api/v1/signup', { username, password });
  58. return response.data;
  59. } catch (error) {
  60. console.error('Error signing up:', error);
  61. return { success: false, error: error.toString() };
  62. }
  63. }
  64. async function ajaxLogin(username, password) {
  65. try {
  66. const response = await axios.post('/api/v1/login', { username, password });
  67. return response.data;
  68. } catch (error) {
  69. console.log('Error logging in:', error.response.data.error);
  70. return { success: false, error: error.response.data.error };
  71. }
  72. }
  73. async function ajaxLogout() {
  74. try {
  75. const response = await axios.post('/api/v1/logout');
  76. return response.data;
  77. } catch (error) {
  78. console.error('Error logging out:', error);
  79. return { success: false, error:error.response.data.error };
  80. }
  81. }
  82. async function getWeather(lat, lon) {
  83. try {
  84. const response = await axios.get("/api/v1/weather", {
  85. params: {
  86. lat: lat,
  87. lon: lon,
  88. },
  89. });
  90. return response.data;
  91. } catch (error) {
  92. console.error("Failed to fetch weather data:", error);
  93. return { success: false, error: "Failed to fetch weather data" };
  94. }
  95. }
  96. function fetchWeatherForCurrentLocation() {
  97. return new Promise((resolve, reject) => {
  98. if ("geolocation" in navigator) {
  99. navigator.geolocation.getCurrentPosition(
  100. async (position) => {
  101. const lat = position.coords.latitude;
  102. const lon = position.coords.longitude;
  103. try {
  104. const weatherData = await getWeather(lat, lon);
  105. if (weatherData.success) {
  106. console.log("Weather data:", weatherData.data);
  107. resolve(weatherData);
  108. } else {
  109. reject("Failed to fetch weather data");
  110. }
  111. } catch (error) {
  112. reject(error);
  113. }
  114. },
  115. (error) => {
  116. console.error("Error getting location:", error);
  117. reject("Error getting location");
  118. }
  119. );
  120. } else {
  121. console.error("Geolocation is not supported by this browser.");
  122. reject("Geolocation is not supported by this browser.");
  123. }
  124. });
  125. }