forked from DGNum/metis
53 lines
1.2 KiB
JavaScript
53 lines
1.2 KiB
JavaScript
|
import ICAL from 'ical.js';
|
||
|
|
||
|
const calendarIds = [
|
||
|
"5WrcagPPARQ3BD87"
|
||
|
];
|
||
|
|
||
|
function mkCalendarUrl(id) {
|
||
|
return `/cal/${id}/?export&accept=jcal`;
|
||
|
}
|
||
|
|
||
|
function fetchCalendar(id) {
|
||
|
return fetch(mkCalendarUrl(id), { credentials: 'omit' }).then(resp => resp.json()).catch(err => console.error(err));
|
||
|
}
|
||
|
|
||
|
class Calendar {
|
||
|
constructor (calendar) {
|
||
|
this.calName = calendar[1][3][3];
|
||
|
this.events = calendar[2].filter(item => item[0] === 'vevent').map(item => this._parse_vevent(item[1]));
|
||
|
}
|
||
|
|
||
|
_parse_vevent(vevent) {
|
||
|
const event = {};
|
||
|
vevent.forEach(elt => {
|
||
|
event[elt[0]] = elt[3];
|
||
|
});
|
||
|
return event;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function mkEventsFromCalendar(id) {
|
||
|
return fetchCalendar(id).then(calendar => {
|
||
|
if (calendar[0] !== 'vcalendar') return;
|
||
|
const cal = new Calendar(calendar)
|
||
|
return cal.events.map(evt => ({ title: evt.summary, start: new Date(evt.dtstart), end: new Date(evt.dtend) }))
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export function mkEvent(title, start, duration, ...rest) {
|
||
|
start = new Date(start)
|
||
|
const end = new Date(start)
|
||
|
end.setMinutes(start.getMinutes() + duration)
|
||
|
return {
|
||
|
title,
|
||
|
start,
|
||
|
end,
|
||
|
...rest
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function refreshEvents() {
|
||
|
return Promise.all(calendarIds.map(mkEventsFromCalendar))
|
||
|
}
|