calendar widget 4
This commit is contained in:
parent
47de44a050
commit
0df69f6969
@ -36,51 +36,78 @@ export function CalendarWidget() {
|
|||||||
// Fetch calendars with events
|
// Fetch calendars with events
|
||||||
const response = await fetch('/api/calendars');
|
const response = await fetch('/api/calendars');
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
console.error("Calendar API response not OK:", response.status, response.statusText);
|
||||||
throw new Error("Impossible de charger les événements");
|
throw new Error("Impossible de charger les événements");
|
||||||
}
|
}
|
||||||
|
|
||||||
const calendarsData = await response.json();
|
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
|
// Get current date at the start of the day
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
now.setHours(0, 0, 0, 0);
|
now.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
// Extract all events and add calendar info
|
// Extract all events and add calendar info
|
||||||
const allEvents = calendarsData.flatMap((calendar: any) =>
|
const allEvents = calendarsData.flatMap((calendar) => {
|
||||||
(calendar.events || []).map((event: any) => ({
|
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,
|
id: event.id,
|
||||||
title: event.title,
|
title: event.title,
|
||||||
start: new Date(event.start),
|
start: startDate,
|
||||||
end: new Date(event.end),
|
end: endDate,
|
||||||
isAllDay: event.isAllDay,
|
isAllDay: event.isAllDay,
|
||||||
calendarId: event.calendarId,
|
calendarId: event.calendarId,
|
||||||
calendarColor: calendar.color,
|
calendarColor: calendar.color,
|
||||||
calendarName: calendar.name
|
calendarName: calendar.name
|
||||||
}))
|
};
|
||||||
);
|
});
|
||||||
|
});
|
||||||
|
|
||||||
console.log("Processed events:", allEvents);
|
console.log("All processed events:", allEvents);
|
||||||
|
|
||||||
// Filter for upcoming events (today and future)
|
// Filter for upcoming events (today and future)
|
||||||
const upcomingEvents = allEvents
|
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())
|
.sort((a, b) => a.start.getTime() - b.start.getTime())
|
||||||
.slice(0, 5);
|
.slice(0, 5);
|
||||||
|
|
||||||
console.log("Filtered upcoming events:", upcomingEvents);
|
console.log("Final upcoming events:", upcomingEvents);
|
||||||
setEvents(upcomingEvents);
|
setEvents(upcomingEvents);
|
||||||
} catch (err) {
|
} 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");
|
setError("Impossible de charger les événements à venir");
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Initial fetch
|
||||||
fetchUpcomingEvents();
|
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);
|
const intervalId = setInterval(fetchUpcomingEvents, 300000);
|
||||||
|
|
||||||
return () => clearInterval(intervalId);
|
return () => clearInterval(intervalId);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user