2022-03-06 00:59:11 +01:00
|
|
|
import ICAL from 'ical.js'
|
2022-02-20 19:16:33 +01:00
|
|
|
|
2022-03-01 17:28:32 +01:00
|
|
|
const clouds = {
|
2022-03-06 00:59:11 +01:00
|
|
|
KLUB_RESEAU: 'klub-reseau',
|
|
|
|
ELEVES_ENS: 'eleves-ens'
|
|
|
|
}
|
2022-03-01 17:28:32 +01:00
|
|
|
|
2022-02-20 22:29:15 +01:00
|
|
|
const calendars = {
|
2022-03-06 00:59:11 +01:00
|
|
|
'5WrcagPPARQ3BD87': {
|
2022-03-01 17:28:32 +01:00
|
|
|
cloud: clouds.KLUB_RESEAU,
|
2022-03-06 00:59:11 +01:00
|
|
|
name: 'Club réseau',
|
2022-02-20 22:29:15 +01:00
|
|
|
color: null
|
|
|
|
},
|
2022-03-06 00:59:11 +01:00
|
|
|
TFEAKjAgNFQZpNjo: {
|
2022-03-01 17:28:32 +01:00
|
|
|
cloud: clouds.KLUB_RESEAU,
|
2022-03-06 00:59:11 +01:00
|
|
|
name: 'hackENS',
|
2022-02-20 22:29:15 +01:00
|
|
|
color: null
|
2022-03-01 17:28:32 +01:00
|
|
|
},
|
2022-03-06 00:59:11 +01:00
|
|
|
LLWm8qK9iC5YGrrR: {
|
2022-03-01 17:28:32 +01:00
|
|
|
cloud: clouds.ELEVES_ENS,
|
2022-03-06 00:59:11 +01:00
|
|
|
name: 'Délégation Générale',
|
|
|
|
short_name: 'DG',
|
2022-03-01 17:28:32 +01:00
|
|
|
color: null
|
2022-03-06 00:59:11 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 22:29:15 +01:00
|
|
|
|
|
|
|
export const calendarTree = {
|
2022-03-06 00:59:11 +01:00
|
|
|
'Clubs COF': {
|
|
|
|
'Club réseau': {},
|
|
|
|
hackENS: {}
|
2022-03-05 02:38:58 +01:00
|
|
|
},
|
2022-03-06 00:59:11 +01:00
|
|
|
COF: {
|
|
|
|
BDA: {
|
|
|
|
Philharmonie: {}
|
2022-03-05 22:54:24 +01:00
|
|
|
},
|
2022-03-06 00:59:11 +01:00
|
|
|
AG: {}
|
2022-03-05 22:54:24 +01:00
|
|
|
},
|
2022-03-06 00:59:11 +01:00
|
|
|
BDS: {},
|
|
|
|
'Délégation Générale': {}
|
|
|
|
}
|
2022-02-20 22:29:15 +01:00
|
|
|
|
2022-03-06 00:59:11 +01:00
|
|
|
const calendarIds = Object.keys(calendars)
|
2022-02-20 19:16:33 +01:00
|
|
|
|
2022-03-01 17:28:32 +01:00
|
|
|
function mkCalendarUrl(id, { cloud }) {
|
2022-03-06 00:59:11 +01:00
|
|
|
return `/cal/${cloud}/${id}/?export&accept=jcal`
|
2022-02-20 19:16:33 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 17:28:32 +01:00
|
|
|
function fetchCalendar(id, cal) {
|
2022-03-06 00:59:11 +01:00
|
|
|
return fetch(mkCalendarUrl(id, cal), { credentials: 'omit' })
|
|
|
|
.then(resp => resp.json())
|
|
|
|
.catch(err => console.error(err))
|
2022-02-20 19:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class Calendar {
|
2022-03-06 00:59:11 +01:00
|
|
|
constructor(id, calendar) {
|
|
|
|
const metadata = calendars[id]
|
|
|
|
this.name = metadata.name
|
|
|
|
this.color = metadata.color || calendar[1][4][3]
|
|
|
|
this.events = calendar[2]
|
|
|
|
.filter(item => item[0] === 'vevent')
|
|
|
|
.map(item => this._parse_vevent(item[1]))
|
2022-02-20 19:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_parse_vevent(vevent) {
|
2022-03-06 00:59:11 +01:00
|
|
|
const event = {}
|
2022-02-20 19:16:33 +01:00
|
|
|
vevent.forEach(elt => {
|
2022-03-06 00:59:11 +01:00
|
|
|
event[elt[0]] = elt[3]
|
|
|
|
})
|
|
|
|
return event
|
2022-02-20 19:16:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 22:29:15 +01:00
|
|
|
function fcEventFromjCalEvent(cal) {
|
|
|
|
return function (evt) {
|
2022-03-06 00:59:11 +01:00
|
|
|
const start = new Date(evt.dtstart)
|
|
|
|
const end = new Date(evt.dtend)
|
2022-02-20 22:29:15 +01:00
|
|
|
const fcEvent = {
|
|
|
|
title: `${cal.name}: ${evt.summary}`,
|
2022-03-01 17:28:32 +01:00
|
|
|
start: evt.dtstart,
|
|
|
|
end: evt.dtend,
|
2022-02-20 22:29:15 +01:00
|
|
|
color: cal.color,
|
|
|
|
duration: end - start // in ms
|
2022-03-06 00:59:11 +01:00
|
|
|
}
|
2022-02-20 22:29:15 +01:00
|
|
|
|
|
|
|
if (evt.description) {
|
2022-03-06 00:59:11 +01:00
|
|
|
fcEvent.description = evt.description
|
|
|
|
}
|
|
|
|
|
|
|
|
if (evt.location) {
|
|
|
|
fcEvent.location = evt.location
|
2022-02-20 22:29:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (evt.rrule) {
|
2022-03-06 00:59:11 +01:00
|
|
|
const { freq, byday, interval } = evt.rrule
|
2022-02-20 22:29:15 +01:00
|
|
|
fcEvent.rrule = {
|
|
|
|
freq,
|
|
|
|
byweekday: byday,
|
2022-03-06 00:59:11 +01:00
|
|
|
dtstart: evt.dtstart
|
|
|
|
}
|
2022-02-21 22:13:44 +01:00
|
|
|
|
|
|
|
if (interval) {
|
2022-03-06 00:59:11 +01:00
|
|
|
fcEvent.rrule.interval = interval
|
2022-02-21 22:13:44 +01:00
|
|
|
}
|
2022-02-20 22:29:15 +01:00
|
|
|
}
|
|
|
|
|
2022-03-06 00:59:11 +01:00
|
|
|
return fcEvent
|
2022-02-20 22:29:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 17:28:32 +01:00
|
|
|
function mkEventsFromCalendar(id, cal) {
|
|
|
|
return fetchCalendar(id, cal).then(calendar => {
|
2022-03-06 00:59:11 +01:00
|
|
|
if (calendar[0] !== 'vcalendar') return
|
2022-02-20 22:29:15 +01:00
|
|
|
const cal = new Calendar(id, calendar)
|
|
|
|
return cal.events.map(fcEventFromjCalEvent(cal))
|
2022-03-06 00:59:11 +01:00
|
|
|
})
|
2022-02-20 19:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 22:29:15 +01:00
|
|
|
// TODO: move to FullCalendar custom fetcher to control start&end.
|
|
|
|
export function refreshEvents(selectedCalendars) {
|
2022-03-01 17:28:32 +01:00
|
|
|
return Promise.all(
|
|
|
|
calendarIds
|
2022-03-06 00:59:11 +01:00
|
|
|
.filter(id =>
|
|
|
|
selectedCalendars ? selectedCalendars.includes(calendars[id].name) : true
|
|
|
|
)
|
2022-03-01 17:28:32 +01:00
|
|
|
.map(id => mkEventsFromCalendar(id, calendars[id]))
|
2022-03-06 00:59:11 +01:00
|
|
|
)
|
2022-02-20 19:16:33 +01:00
|
|
|
}
|