calendar widget 3

This commit is contained in:
Alma 2025-04-13 18:24:10 +02:00
parent 27ce371dea
commit 47de44a050

View File

@ -33,36 +33,42 @@ export function CalendarWidget() {
try {
setLoading(true);
// Fetch calendars with events from the correct endpoint
// Fetch calendars with events
const response = await fetch('/api/calendars');
if (!response.ok) {
throw new Error("Impossible de charger les événements");
}
const calendarsData = await response.json();
console.log("Fetched calendars data:", calendarsData);
// Extract and process events from all calendars
// Get current date at the start of the day
const now = new Date();
now.setHours(0, 0, 0, 0);
// Extract all events and add calendar info
const allEvents = calendarsData.flatMap((calendar: any) =>
(calendar.events || []).map((event: any) => ({
...event,
calendarColor: calendar.color,
calendarName: calendar.name,
id: event.id,
title: event.title,
start: new Date(event.start),
end: new Date(event.end)
end: new Date(event.end),
isAllDay: event.isAllDay,
calendarId: event.calendarId,
calendarColor: calendar.color,
calendarName: calendar.name
}))
);
// Filter events for the next 7 days
const now = new Date();
const nextWeek = addDays(now, 7);
console.log("Processed events:", allEvents);
// Filter for upcoming events (today and future)
const upcomingEvents = allEvents
.filter((event: Event) => {
const eventStart = new Date(event.start);
return eventStart >= now && eventStart <= nextWeek;
})
.sort((a: Event, b: Event) => a.start.getTime() - b.start.getTime())
.filter(event => event.start >= now)
.sort((a, b) => a.start.getTime() - b.start.getTime())
.slice(0, 5);
console.log("Filtered upcoming events:", upcomingEvents);
setEvents(upcomingEvents);
} catch (err) {
console.error("Erreur lors du chargement des événements:", err);
@ -74,8 +80,8 @@ export function CalendarWidget() {
fetchUpcomingEvents();
// Set up an interval to refresh events every minute
const intervalId = setInterval(fetchUpcomingEvents, 60000);
// Set up an interval to refresh events every 5 minutes instead of every minute
const intervalId = setInterval(fetchUpcomingEvents, 300000);
return () => clearInterval(intervalId);
}, [session]);