calendar widget 4
This commit is contained in:
parent
47de44a050
commit
0df69f6969
@ -36,51 +36,78 @@ export function CalendarWidget() {
|
||||
// Fetch calendars with events
|
||||
const response = await fetch('/api/calendars');
|
||||
if (!response.ok) {
|
||||
console.error("Calendar API response not OK:", response.status, response.statusText);
|
||||
throw new Error("Impossible de charger les événements");
|
||||
}
|
||||
|
||||
const calendarsData = await response.json();
|
||||
console.log("Fetched calendars data:", calendarsData);
|
||||
console.log("Raw calendars data:", calendarsData);
|
||||
|
||||
if (!Array.isArray(calendarsData)) {
|
||||
console.error("Calendars data is not an array:", calendarsData);
|
||||
throw new Error("Format de données invalide");
|
||||
}
|
||||
|
||||
// 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) => ({
|
||||
id: event.id,
|
||||
title: event.title,
|
||||
start: new Date(event.start),
|
||||
end: new Date(event.end),
|
||||
isAllDay: event.isAllDay,
|
||||
calendarId: event.calendarId,
|
||||
calendarColor: calendar.color,
|
||||
calendarName: calendar.name
|
||||
}))
|
||||
);
|
||||
const allEvents = calendarsData.flatMap((calendar) => {
|
||||
console.log("Processing calendar:", calendar.name, "Events:", calendar.events?.length || 0);
|
||||
return (calendar.events || []).map((event) => {
|
||||
const startDate = new Date(event.start);
|
||||
const endDate = new Date(event.end);
|
||||
console.log("Processing event:", {
|
||||
title: event.title,
|
||||
start: startDate,
|
||||
isAllDay: event.isAllDay,
|
||||
calendarName: calendar.name
|
||||
});
|
||||
return {
|
||||
id: event.id,
|
||||
title: event.title,
|
||||
start: startDate,
|
||||
end: endDate,
|
||||
isAllDay: event.isAllDay,
|
||||
calendarId: event.calendarId,
|
||||
calendarColor: calendar.color,
|
||||
calendarName: calendar.name
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
console.log("Processed events:", allEvents);
|
||||
console.log("All processed events:", allEvents);
|
||||
|
||||
// Filter for upcoming events (today and future)
|
||||
const upcomingEvents = allEvents
|
||||
.filter(event => event.start >= now)
|
||||
.filter(event => {
|
||||
const isUpcoming = event.start >= now;
|
||||
console.log("Event filter check:", {
|
||||
title: event.title,
|
||||
start: event.start,
|
||||
isUpcoming,
|
||||
now
|
||||
});
|
||||
return isUpcoming;
|
||||
})
|
||||
.sort((a, b) => a.start.getTime() - b.start.getTime())
|
||||
.slice(0, 5);
|
||||
|
||||
console.log("Filtered upcoming events:", upcomingEvents);
|
||||
console.log("Final upcoming events:", upcomingEvents);
|
||||
setEvents(upcomingEvents);
|
||||
} catch (err) {
|
||||
console.error("Erreur lors du chargement des événements:", err);
|
||||
console.error("Error in fetchUpcomingEvents:", err);
|
||||
setError("Impossible de charger les événements à venir");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// Initial fetch
|
||||
fetchUpcomingEvents();
|
||||
|
||||
// Set up an interval to refresh events every 5 minutes instead of every minute
|
||||
// Set up an interval to refresh events every 5 minutes
|
||||
const intervalId = setInterval(fetchUpcomingEvents, 300000);
|
||||
|
||||
return () => clearInterval(intervalId);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user