api_calls.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. console.log(response.data)
  32. return response.data;
  33. } catch (error) {
  34. console.error('Error fetching session from backend:', error);
  35. return { success: false, data: null, error: error.toString() };
  36. }
  37. }
  38. async function getVotesFromBackend() {
  39. try {
  40. const response = await axios.get('/api/v1/votes/list');
  41. return response.data;
  42. } catch (error) {
  43. console.error('Error fetching votes from backend:', error);
  44. return { success: false, data: null, error: error.toString() };
  45. }
  46. }
  47. async function setMyVote(day, vote) {
  48. // this is a placeholder. rewrite it completely. (you can ignore this for Pass tier)
  49. if (!fake_local_session) return false;
  50. let me = fake_local_session.username;
  51. let dayofvotes = fake_local_votesdata.filter(vd => vd.date === day)[0];
  52. if (!me || !dayofvotes) return false;
  53. if (vote === 'maybe') {
  54. delete dayofvotes.votes[me];
  55. } else {
  56. dayofvotes.votes[me] = vote;
  57. }
  58. return { success: true };
  59. }
  60. async function ajaxSignup(username, password) {
  61. try {
  62. const response = await axios.post('/api/v1/signup', { username, password });
  63. return response.data;
  64. } catch (error) {
  65. console.error('Error signing up:', error);
  66. return { success: false, error: error.toString() };
  67. }
  68. }
  69. async function ajaxLogin(username, password) {
  70. try {
  71. const response = await axios.post('/api/v1/login', { username, password });
  72. return response.data;
  73. } catch (error) {
  74. console.error('Error logging in:', error);
  75. return { success: false, error: error.toString() };
  76. }
  77. }
  78. async function ajaxLogout() {
  79. try {
  80. const response = await axios.post('/api/v1/logout');
  81. return response.data;
  82. } catch (error) {
  83. console.error('Error logging out:', error);
  84. return { success: false, error: error.toString() };
  85. }
  86. }