Fix: show recurring and multi-day events (#2451)

This commit is contained in:
Denis Papec 2023-12-10 15:19:43 +00:00 committed by GitHub
parent 0a3d552114
commit a72ccb6d27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 21 deletions

View file

@ -2,6 +2,7 @@ import { DateTime } from "luxon";
import { parseString } from "cal-parser";
import { useEffect } from "react";
import { useTranslation } from "next-i18next";
import { RRule } from "rrule";
import useWidgetAPI from "../../../utils/proxy/use-widget-api";
import Error from "../../../components/services/widget/error";
@ -22,15 +23,15 @@ export default function Integration({ config, params, setEvents, hideErrors }) {
}
}
if (icalError || !parsedIcal) {
const startDate = DateTime.fromISO(params.start);
const endDate = DateTime.fromISO(params.end);
if (icalError || !parsedIcal || !startDate.isValid || !endDate.isValid) {
return;
}
const eventsToAdd = {};
const events = parsedIcal?.getEventsBetweenDates(
DateTime.fromISO(params.start).toJSDate(),
DateTime.fromISO(params.end).toJSDate(),
);
const events = parsedIcal?.getEventsBetweenDates(startDate.toJSDate(), endDate.toJSDate());
events?.forEach((event) => {
let title = `${event?.summary?.value}`;
@ -38,16 +39,31 @@ export default function Integration({ config, params, setEvents, hideErrors }) {
title = `${config.name}: ${title}`;
}
event.matchingDates.forEach((date) => {
eventsToAdd[event?.uid?.value] = {
title,
date: DateTime.fromJSDate(date),
color: config?.color ?? "zinc",
isCompleted: DateTime.fromJSDate(date) < DateTime.now(),
additional: event.location?.value,
type: "ical",
};
});
const eventToAdd = (date, i, type) => {
const duration = event.dtend.value - event.dtstart.value;
const days = duration / (1000 * 60 * 60 * 24);
for (let j = 0; j < days; j += 1) {
eventsToAdd[`${event?.uid?.value}${i}${j}${type}`] = {
title,
date: DateTime.fromJSDate(date).plus({ days: j }),
color: config?.color ?? "zinc",
isCompleted: DateTime.fromJSDate(date) < DateTime.now(),
additional: event.location?.value,
type: "ical",
};
}
};
if (event?.recurrenceRule?.options) {
const rule = new RRule(event.recurrenceRule.options);
const recurringEvents = rule.between(startDate.toJSDate(), endDate.toJSDate());
recurringEvents.forEach((date, i) => eventToAdd(date, i, "recurring"));
return;
}
event.matchingDates.forEach((date, i) => eventToAdd(date, i, "single"));
});
setEvents((prevEvents) => ({ ...prevEvents, ...eventsToAdd }));