Neah version calendar fix 3 debuger ???? ??????
This commit is contained in:
parent
c9f7125f37
commit
48c3ba470c
@ -27,50 +27,43 @@ export function CalendarWidget() {
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
console.log("Calendar Widget - Session Status:", status);
|
||||
console.log("Calendar Widget - Session Data:", session);
|
||||
|
||||
if (status === "loading") {
|
||||
console.log("Calendar Widget - Session is loading");
|
||||
return;
|
||||
}
|
||||
|
||||
if (status !== "authenticated" || !session) {
|
||||
console.log("Calendar Widget - Not authenticated, skipping fetch");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchUpcomingEvents = async () => {
|
||||
try {
|
||||
console.log("Calendar Widget - Starting to fetch events");
|
||||
setLoading(true);
|
||||
|
||||
// Fetch calendars with events
|
||||
console.log("Calendar Widget - Making API request to /api/calendars");
|
||||
const response = await fetch('/api/calendars');
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("Calendar Widget - API response not OK:", response.status, response.statusText);
|
||||
throw new Error("Impossible de charger les événements");
|
||||
}
|
||||
|
||||
const calendarsData = await response.json();
|
||||
console.log("Calendar Widget - Raw calendars data:", calendarsData);
|
||||
|
||||
if (!Array.isArray(calendarsData)) {
|
||||
console.error("Calendar Widget - 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) => {
|
||||
console.log("Calendar Widget - Processing calendar:", calendar.name, "Events:", calendar.events?.length || 0);
|
||||
return (calendar.events || []).map((event) => {
|
||||
return (calendar.events || []).map((event: {
|
||||
id: string;
|
||||
title: string;
|
||||
start: string;
|
||||
end: string;
|
||||
isAllDay: boolean;
|
||||
calendarId: string;
|
||||
}) => {
|
||||
const startDate = new Date(event.start);
|
||||
const endDate = new Date(event.end);
|
||||
return {
|
||||
@ -86,27 +79,22 @@ export function CalendarWidget() {
|
||||
});
|
||||
});
|
||||
|
||||
// Filter for upcoming events (today and future)
|
||||
const upcomingEvents = allEvents
|
||||
.filter(event => event.start >= now)
|
||||
.sort((a, b) => a.start.getTime() - b.start.getTime())
|
||||
.slice(0, 5);
|
||||
|
||||
console.log("Calendar Widget - Final upcoming events:", upcomingEvents);
|
||||
setEvents(upcomingEvents);
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
console.error("Calendar Widget - 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
|
||||
const intervalId = setInterval(fetchUpcomingEvents, 300000);
|
||||
|
||||
return () => clearInterval(intervalId);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user