import {defineStore} from 'pinia'; import moment from 'moment-timezone'; import axios from 'axios'; moment.tz.setDefault('America/Chicago') // const headers = { // "Access-Control-Allow-Origin": "*", // } const apikey = import.meta.env.VITE_API_KEY; const apiUrl = import.meta.env.PROD ? "https://api.digisnaxx.com" : "http://localhost:8000"; export const useEventsStore = defineStore('Events', { state: () => { return { headers: { "content-type": "application/json", "Authorization": `${apikey}`, }, events: [], promo: [], errors: [], search_term: '', the_day: "", new_msg: "", today: "", shortcode: "", }; }, actions: { // setCalendar(cal) { // this.shortcode = cal; // console.log("CAL: ", this.shortcode); // return this.shortcode; // }, async fetchPromo() { // const results = await fetch(`${apiUrl}/socials/promo/`, {headers: this.headers}); const results = await fetch(`${apiUrl}/socials/links/`, {headers: this.headers}); const data = await results.json(); this.promo = data; }, async fetchEvents(cal) { this.shortcode = cal; const results = await fetch(`${apiUrl}/events/events/?calendar__shortcode=${this.shortcode}`, {headers: this.headers}); const data = await results.json(); const events_list = data.slice(0,126); const index = 6; const value = {}; const newArr = []; for (let i = 0; i < events_list.length; i++) { if (i === index) { newArr.push(value); } newArr.push(events_list[i]); }; this.events = newArr; this.the_day = ""; if(this.events.length() == 0){ this.fetchEvents(cal) } }, async get_today(){ var currentDate = moment().format('MMMM Do, YYYY | h:mm a'); this.today = currentDate; }, searchEvents(){ console.log("Running Search"); let api_url = `${apiUrl}/events/events/`; if(this.search_term!==''||this.search_term!==null) { api_url = `${apiUrl}/events/events/?search=${this.search_term}&calendar__shortcode=${this.shortcode}`; this.new_msg = "Click Search again to view all events." } axios.get(api_url, {headers: this.headers}) .then(response => { this.events = response.data if(api_url==`${apiUrl}/events/events/?search=`){ this.new_msg = "" } }) .catch(e => { this.errors.push(e) }) }, getEventByDate(which_day) { let today = moment().format("YYYY-MM-DD"); let the_date = moment().add(which_day, 'd').format("YYYY-MM-DD"); this.the_day = the_date; let api_url = `${apiUrl}/events/events/`; if(the_date!==''||the_date!==null) { api_url = `${apiUrl}/events/events/?show_day=${the_date}&calendar__shortcode=${this.shortcode}`; } axios.get(api_url, {headers: this.headers}) .then(response => { this.events = response.data }) .catch(e => { this.errors.push(e) }) }, async getEventsByType(event_type){ let api_url = `${apiUrl}/events/events/`; if(this.the_day!=='' && (event_type!==''||event_type!==null)) { api_url = `${apiUrl}/events/events/?event_type=${event_type}&show_day=${this.the_day}&calendar__shortcode=${this.shortcode}`; } else if(this.the_day=='' && (event_type!==''||event_type!==null)) { api_url = `${apiUrl}/events/events/?event_type=${event_type}&calendar__shortcode=${this.shortcode}`; } console.log(api_url) const result = await fetch(api_url, {headers: this.headers}); const data = await result.json(); this.events = data; this.fetchPromo() return this.events; } } });