| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- //static/js/api_calls.js
- let fake_local_session = { username: 'nobody' };
- let fake_local_votesdata = [
- {
- date: "2024-04-01",
- votes: {
- 'who': "yes",
- 'what': "no",
- },
- },
- {
- date: "2024-04-02",
- votes: {
- 'what': "no",
- 'when': "yes",
- 'where': "yes",
- },
- },
- {
- date: "2024-04-03",
- votes: {
- 'how': "no",
- 'random': Math.random() < 0.5 ? "no" : "yes",
- },
- },
- ]
- ////// This line, and everything above this line, should be deleted when you get the AJAX working. Really.
- async function getSessionFromBackend() {
- try {
- const response = await axios.get('/api/v1/getSession');
- console.log(response.data)
- return response.data;
- } catch (error) {
- console.error('Error fetching session from backend:', error);
- return { success: false, data: null, error: error.toString() };
- }
- }
- async function getVotesFromBackend() {
- try {
- const response = await axios.get('/api/v1/votes/list');
- return response.data;
- } catch (error) {
- console.error('Error fetching votes from backend:', error);
- return { success: false, data: null, error: error.toString() };
- }
- }
- async function setMyVote(day, vote) {
- // this is a placeholder. rewrite it completely. (you can ignore this for Pass tier)
- if (!fake_local_session) return false;
- let me = fake_local_session.username;
- let dayofvotes = fake_local_votesdata.filter(vd => vd.date === day)[0];
- if (!me || !dayofvotes) return false;
- if (vote === 'maybe') {
- delete dayofvotes.votes[me];
- } else {
- dayofvotes.votes[me] = vote;
- }
- return { success: true };
- }
- async function ajaxSignup(username, password) {
- try {
- const response = await axios.post('/api/v1/signup', { username, password });
- return response.data;
- } catch (error) {
- console.error('Error signing up:', error);
- return { success: false, error: error.toString() };
- }
- }
- async function ajaxLogin(username, password) {
- try {
- const response = await axios.post('/api/v1/login', { username, password });
- return response.data;
- } catch (error) {
- console.error('Error logging in:', error);
- return { success: false, error: error.toString() };
- }
- }
- async function ajaxLogout() {
- try {
- const response = await axios.post('/api/v1/logout');
- return response.data;
- } catch (error) {
- console.error('Error logging out:', error);
- return { success: false, error: error.toString() };
- }
- }
|