| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //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');
- 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(username, date, vote) {
- try {
- const response = await axios.post('/api/v1/votes/set', { username, date, vote });
- return response.data;
- } catch (error) {
- console.error('Error setting vote:', error.response ? error.response.data.error : error);
- return { success: false, error: error.response ? error.response.data.error : 'Unknown error' };
- }
- }
- 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.log('Error logging in:', error.response.data.error);
- return { success: false, error: error.response.data.error };
- }
- }
- 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.response.data.error };
- }
- }
- async function getWeather(lat, lon) {
- try {
- const response = await axios.get("/api/v1/weather", {
- params: {
- lat: lat,
- lon: lon,
- },
- });
- return response.data;
- } catch (error) {
- console.error("Failed to fetch weather data:", error);
- return { success: false, error: "Failed to fetch weather data" };
- }
- }
- function fetchWeatherForCurrentLocation() {
- return new Promise((resolve, reject) => {
- if ("geolocation" in navigator) {
- navigator.geolocation.getCurrentPosition(
- async (position) => {
- const lat = position.coords.latitude;
- const lon = position.coords.longitude;
- try {
- const weatherData = await getWeather(lat, lon);
- if (weatherData.success) {
- console.log("Weather data:", weatherData.data);
- resolve(weatherData);
- } else {
- reject("Failed to fetch weather data");
- }
- } catch (error) {
- reject(error);
- }
- },
- (error) => {
- console.error("Error getting location:", error);
- reject("Error getting location");
- }
- );
- } else {
- console.error("Geolocation is not supported by this browser.");
- reject("Geolocation is not supported by this browser.");
- }
- });
- }
|