wrbapp/frontend/stores/calendarStore.js

53 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2023-03-20 11:23:46 +01:00
export const useCalendarStore = defineStore('calendar', () => {
const events = ref([])
const getEvents = async (group) => {
let fridayEvents = []
let saturdayEvents = []
let matchEvents = []
if (group.includes('Zaterdag')){
let data = await fetch('https://www.googleapis.com/calendar/v3/calendars/c_astg0d0auheip5o4269v1qvv3g@group.calendar.google.com/events?key=AIzaSyBLmxWNEnbkiW_0c2UrsHMbxXv73dX-KYw')
if (!data.ok){
throw Error('No data available')
}
const jsonEvents = await data.json()
saturdayEvents = jsonEvents.items
} if (group.includes('Vrijdag')){
let data = await fetch('https://www.googleapis.com/calendar/v3/calendars/c_u3895n01jt8qnusm7f6pmqjb0k%40group.calendar.google.com/events?key=AIzaSyBLmxWNEnbkiW_0c2UrsHMbxXv73dX-KYw')
if (!data.ok){
throw Error('No data available')
}
const jsonEvents = await data.json()
fridayEvents = jsonEvents.items
} if (group.includes('Wedstrijd')){
let data = await fetch('https://www.googleapis.com/calendar/v3/calendars/c_c2296iboq07n24galuobeesovs@group.calendar.google.com/events?key=AIzaSyBLmxWNEnbkiW_0c2UrsHMbxXv73dX-KYw')
if (!data.ok){
throw Error('No data available')
}
const jsonEvents = await data.json()
matchEvents = jsonEvents.items
}
const allEvents = [...fridayEvents, ...saturdayEvents, ...matchEvents]
const now = new Date()
now.setHours(0, 0, 0, 0)
const sortedEvents = allEvents.sort((a, b) => new Date(a.start.dateTime) - new Date(b.start.dateTime))
const filteredEvents = sortedEvents.filter((event) => new Date(event.start.dateTime) > now )
filteredEvents.forEach(element => {
events.value.push({
description: element.description,
date: new Date(element.start.dateTime)
})
});
}
return { events, getEvents }
})