Add location to events

This commit is contained in:
Tom Hubrecht 2022-03-06 00:59:11 +01:00
parent 587e29ec6a
commit 03e284b7dc

View file

@ -1,110 +1,118 @@
import ICAL from 'ical.js'; import ICAL from 'ical.js'
const clouds = { const clouds = {
KLUB_RESEAU: "klub-reseau", KLUB_RESEAU: 'klub-reseau',
ELEVES_ENS: "eleves-ens" ELEVES_ENS: 'eleves-ens'
}; }
const calendars = { const calendars = {
"5WrcagPPARQ3BD87": { '5WrcagPPARQ3BD87': {
cloud: clouds.KLUB_RESEAU, cloud: clouds.KLUB_RESEAU,
name: "Club réseau", name: 'Club réseau',
color: null color: null
}, },
"TFEAKjAgNFQZpNjo": { TFEAKjAgNFQZpNjo: {
cloud: clouds.KLUB_RESEAU, cloud: clouds.KLUB_RESEAU,
name: "hackENS", name: 'hackENS',
color: null color: null
}, },
"LLWm8qK9iC5YGrrR": { LLWm8qK9iC5YGrrR: {
cloud: clouds.ELEVES_ENS, cloud: clouds.ELEVES_ENS,
name: "Délégation Générale", name: 'Délégation Générale',
short_name: "DG", short_name: 'DG',
color: null color: null
}, }
}; }
export const calendarTree = { export const calendarTree = {
"Clubs COF": { 'Clubs COF': {
"Club réseau": {}, 'Club réseau': {},
"hackENS": {}, hackENS: {}
}, },
"COF": { COF: {
"BDA": { BDA: {
"Philharmonie": {}, Philharmonie: {}
}, },
"AG": {}, AG: {}
}, },
"BDS": {}, BDS: {},
"Délégation Générale": {}, 'Délégation Générale': {}
}; }
const calendarIds = Object.keys(calendars); const calendarIds = Object.keys(calendars)
function mkCalendarUrl(id, { cloud }) { function mkCalendarUrl(id, { cloud }) {
return `/cal/${cloud}/${id}/?export&accept=jcal`; return `/cal/${cloud}/${id}/?export&accept=jcal`
} }
function fetchCalendar(id, cal) { function fetchCalendar(id, cal) {
return fetch(mkCalendarUrl(id, cal), { credentials: 'omit' }).then(resp => resp.json()).catch(err => console.error(err)); return fetch(mkCalendarUrl(id, cal), { credentials: 'omit' })
.then(resp => resp.json())
.catch(err => console.error(err))
} }
class Calendar { class Calendar {
constructor (id, calendar) { constructor(id, calendar) {
const metadata = calendars[id]; const metadata = calendars[id]
this.name = metadata.name; this.name = metadata.name
this.color = metadata.color || calendar[1][4][3]; this.color = metadata.color || calendar[1][4][3]
this.events = calendar[2].filter(item => item[0] === 'vevent').map(item => this._parse_vevent(item[1])); this.events = calendar[2]
.filter(item => item[0] === 'vevent')
.map(item => this._parse_vevent(item[1]))
} }
_parse_vevent(vevent) { _parse_vevent(vevent) {
const event = {}; const event = {}
vevent.forEach(elt => { vevent.forEach(elt => {
event[elt[0]] = elt[3]; event[elt[0]] = elt[3]
}); })
return event; return event
} }
} }
function fcEventFromjCalEvent(cal) { function fcEventFromjCalEvent(cal) {
return function (evt) { return function (evt) {
const start = new Date(evt.dtstart); const start = new Date(evt.dtstart)
const end = new Date(evt.dtend); const end = new Date(evt.dtend)
const fcEvent = { const fcEvent = {
title: `${cal.name}: ${evt.summary}`, title: `${cal.name}: ${evt.summary}`,
start: evt.dtstart, start: evt.dtstart,
end: evt.dtend, end: evt.dtend,
color: cal.color, color: cal.color,
duration: end - start // in ms duration: end - start // in ms
}; }
if (evt.description) { if (evt.description) {
fcEvent.description = evt.description; fcEvent.description = evt.description
}
if (evt.location) {
fcEvent.location = evt.location
} }
if (evt.rrule) { if (evt.rrule) {
const { freq, byday, interval } = evt.rrule; const { freq, byday, interval } = evt.rrule
fcEvent.rrule = { fcEvent.rrule = {
freq, freq,
byweekday: byday, byweekday: byday,
dtstart: evt.dtstart, dtstart: evt.dtstart
}; }
if (interval) { if (interval) {
fcEvent.rrule.interval = interval; fcEvent.rrule.interval = interval
} }
} }
return fcEvent; return fcEvent
} }
} }
function mkEventsFromCalendar(id, cal) { function mkEventsFromCalendar(id, cal) {
return fetchCalendar(id, cal).then(calendar => { return fetchCalendar(id, cal).then(calendar => {
if (calendar[0] !== 'vcalendar') return; if (calendar[0] !== 'vcalendar') return
const cal = new Calendar(id, calendar) const cal = new Calendar(id, calendar)
return cal.events.map(fcEventFromjCalEvent(cal)) return cal.events.map(fcEventFromjCalEvent(cal))
}); })
} }
export function mkEvent(title, start, duration, ...rest) { export function mkEvent(title, start, duration, ...rest) {
@ -123,7 +131,9 @@ export function mkEvent(title, start, duration, ...rest) {
export function refreshEvents(selectedCalendars) { export function refreshEvents(selectedCalendars) {
return Promise.all( return Promise.all(
calendarIds calendarIds
.filter(id => selectedCalendars ? selectedCalendars.includes(calendars[id].name) : true) .filter(id =>
selectedCalendars ? selectedCalendars.includes(calendars[id].name) : true
)
.map(id => mkEventsFromCalendar(id, calendars[id])) .map(id => mkEventsFromCalendar(id, calendars[id]))
); )
} }