Neah version calendar fix 3 debuger

This commit is contained in:
alma 2025-04-16 21:15:41 +02:00
parent 181bdfaf2a
commit 1198883064
3 changed files with 58 additions and 21 deletions

View File

@ -23,23 +23,40 @@ export function Calendar() {
const router = useRouter();
const fetchEvents = async () => {
console.log('🔍 Calendar Widget - Starting fetchEvents');
setLoading(true);
try {
const response = await fetch('/api/calendars');
console.log('📡 Calendar Widget - Making API request to /api/calendars');
const response = await fetch('/api/calendars', {
credentials: 'include'
});
console.log('📥 Calendar Widget - Response status:', response.status);
if (!response.ok) {
throw new Error('Failed to fetch events');
const errorText = await response.text();
console.error('❌ Calendar Widget - API Error:', {
status: response.status,
statusText: response.statusText,
errorText
});
throw new Error(`Failed to fetch events: ${response.status} ${response.statusText}`);
}
const calendarsData = await response.json();
console.log('Calendar Widget - Fetched calendars:', calendarsData);
console.log('📊 Calendar Widget - Raw API Response:', calendarsData);
// Get current date at the start of the day
const now = new Date();
now.setHours(0, 0, 0, 0);
console.log('📅 Calendar Widget - Current date:', now.toISOString());
// Extract and process events from all calendars
const allEvents = calendarsData.flatMap((calendar: any) =>
(calendar.events || []).map((event: any) => ({
console.log('🔄 Calendar Widget - Processing events from calendars');
const allEvents = calendarsData.flatMap((calendar: any) => {
console.log(`📋 Processing calendar: ${calendar.name} (${calendar.id})`);
return (calendar.events || []).map((event: any) => {
console.log(`📝 Processing event: ${event.title} (${event.id})`);
return {
id: event.id,
title: event.title,
start: event.start,
@ -47,27 +64,41 @@ export function Calendar() {
allDay: event.isAllDay,
calendar: calendar.name,
calendarColor: calendar.color
}))
);
};
});
});
console.log('📈 Calendar Widget - Total events before filtering:', allEvents.length);
// Filter for upcoming events
const upcomingEvents = allEvents
.filter((event: any) => new Date(event.start) >= now)
.sort((a: any, b: any) => new Date(a.start).getTime() - new Date(b.start).getTime())
.filter((event: any) => {
const eventDate = new Date(event.start);
const isUpcoming = eventDate >= now;
console.log(`⏰ Event ${event.title}: ${eventDate.toISOString()} is ${isUpcoming ? 'upcoming' : 'past'}`);
return isUpcoming;
})
.sort((a: any, b: any) => {
const dateA = new Date(a.start);
const dateB = new Date(b.start);
return dateA.getTime() - dateB.getTime();
})
.slice(0, 7);
console.log('Calendar Widget - Processed events:', upcomingEvents);
console.log('✅ Calendar Widget - Final processed events:', upcomingEvents);
setEvents(upcomingEvents);
setError(null);
} catch (err) {
console.error('Error fetching events:', err);
setError('Failed to load events');
console.error('❌ Calendar Widget - Error in fetchEvents:', err);
setError(err instanceof Error ? err.message : 'Failed to load events');
} finally {
console.log('🏁 Calendar Widget - fetchEvents completed');
setLoading(false);
}
};
useEffect(() => {
console.log('🔄 Calendar Widget - Component mounted, fetching events');
fetchEvents();
}, []);

View File

@ -517,7 +517,9 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
const fetchCalendars = async () => {
try {
setLoading(true);
const response = await fetch("/api/calendars");
const response = await fetch("/api/calendars", {
credentials: 'include'
});
if (!response.ok) throw new Error("Failed to fetch calendars");
const data = await response.json();
console.log("Raw calendars data:", data);
@ -571,6 +573,7 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
headers: {
"Content-Type": "application/json",
},
credentials: 'include',
body: JSON.stringify({
...calendarData,
userId,
@ -596,6 +599,7 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
setLoading(true);
const response = await fetch(`/api/calendars/${calendarId}`, {
method: "DELETE",
credentials: 'include'
});
if (!response.ok) {

View File

@ -48,7 +48,9 @@ export function CalendarWidget() {
// Fetch calendars with events
console.log("Calendar Widget - Making API request to /api/calendars");
const response = await fetch('/api/calendars');
const response = await fetch('/api/calendars', {
credentials: 'include'
});
if (!response.ok) {
console.error("Calendar Widget - API response not OK:", response.status, response.statusText);