api_calls.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(day, vote) {
  47. // this is a placeholder. rewrite it completely. (you can ignore this for Pass tier)
  48. if (!fake_local_session) return false;
  49. let me = fake_local_session.username;
  50. let dayofvotes = fake_local_votesdata.filter(vd => vd.date === day)[0];
  51. if (!me || !dayofvotes) return false;
  52. if (vote === 'maybe') {
  53. delete dayofvotes.votes[me];
  54. } else {
  55. dayofvotes.votes[me] = vote;
  56. }
  57. return { success: true };
  58. }
  59. async function ajaxSignup(username, password) {
  60. try {
  61. const response = await axios.post('/api/v1/signup', { username, password });
  62. return response.data;
  63. } catch (error) {
  64. console.error('Error signing up:', error);
  65. return { success: false, error: error.toString() };
  66. }
  67. }
  68. async function ajaxLogin(username, password) {
  69. try {
  70. const response = await axios.post('/api/v1/login', { username, password });
  71. return response.data;
  72. } catch (error) {
  73. console.error('Error logging in:', error);
  74. return { success: false, error: error.toString() };
  75. }
  76. }
  77. async function ajaxLogout() {
  78. try {
  79. const response = await axios.post('/api/v1/logout');
  80. return response.data;
  81. } catch (error) {
  82. console.error('Error logging out:', error);
  83. return { success: false, error: error.toString() };
  84. }
  85. }
  86. async function getWeather(lat, lon) {
  87. try {
  88. const response = await axios.get("/api/v1/weather", {
  89. params: {
  90. lat: lat,
  91. lon: lon,
  92. },
  93. });
  94. return response.data;
  95. } catch (error) {
  96. console.error("Failed to fetch weather data:", error);
  97. return { success: false, error: "Failed to fetch weather data" };
  98. }
  99. }
  100. function fetchWeatherForCurrentLocation() {
  101. return new Promise((resolve, reject) => {
  102. if ("geolocation" in navigator) {
  103. navigator.geolocation.getCurrentPosition(
  104. async (position) => {
  105. const lat = position.coords.latitude;
  106. const lon = position.coords.longitude;
  107. try {
  108. const weatherData = await getWeather(lat, lon);
  109. if (weatherData.success) {
  110. console.log("Weather data:", weatherData.data);
  111. resolve(weatherData);
  112. } else {
  113. reject("Failed to fetch weather data");
  114. }
  115. } catch (error) {
  116. reject(error);
  117. }
  118. },
  119. (error) => {
  120. console.error("Error getting location:", error);
  121. reject("Error getting location");
  122. }
  123. );
  124. } else {
  125. console.error("Geolocation is not supported by this browser.");
  126. reject("Geolocation is not supported by this browser.");
  127. }
  128. });
  129. }