From cbad105ed85319c6aab1c540602acc7a83297d9c Mon Sep 17 00:00:00 2001 From: Alma Date: Sun, 13 Apr 2025 16:01:17 +0200 Subject: [PATCH] calendar 24 --- components/calendar/calendar-client.tsx | 70 +++++++++++++++++-------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/components/calendar/calendar-client.tsx b/components/calendar/calendar-client.tsx index 08ee96fd..9315394c 100644 --- a/components/calendar/calendar-client.tsx +++ b/components/calendar/calendar-client.tsx @@ -608,24 +608,39 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend const startDate = new Date(selectInfo.start); const endDate = new Date(selectInfo.end); - console.log("Date select handler - Current calendars:", calendars); - console.log("Date select handler - Current selectedCalendarId:", selectedCalendarId); + console.log("Date select handler - Current state:", { + calendars: calendars.map(c => ({ id: c.id, name: c.name })), + selectedCalendarId, + availableCalendars: calendars.length + }); // If no calendar is selected, use the first available calendar if (!selectedCalendarId && calendars.length > 0) { - console.log("No calendar selected, selecting first calendar:", calendars[0]); - setSelectedCalendarId(calendars[0].id); + const firstCalendar = calendars[0]; + console.log("No calendar selected, selecting first calendar:", firstCalendar); + setSelectedCalendarId(firstCalendar.id); + + setEventForm({ + title: "", + description: null, + start: startDate.toISOString(), + end: endDate.toISOString(), + allDay: selectInfo.allDay, + location: null, + calendarId: firstCalendar.id + }); + } else { + setEventForm({ + title: "", + description: null, + start: startDate.toISOString(), + end: endDate.toISOString(), + allDay: selectInfo.allDay, + location: null, + calendarId: selectedCalendarId + }); } - setEventForm({ - title: "", - description: null, - start: startDate.toISOString(), - end: endDate.toISOString(), - allDay: selectInfo.allDay, - location: null, - calendarId: selectedCalendarId || calendars[0]?.id, - }); setIsEventModalOpen(true); }; @@ -649,27 +664,40 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend const handleEventSubmit = async () => { try { + // Log the current state before validation + console.log("Current state before submission:", { + selectedCalendarId, + eventForm, + calendars + }); + if (!eventForm.title || !eventForm.start || !eventForm.end || !selectedCalendarId) { console.log("Form validation failed:", { title: eventForm.title, start: eventForm.start, end: eventForm.end, - selectedCalendarId + selectedCalendarId, + calendars: calendars.map(c => ({ id: c.id, name: c.name })) }); setError("Veuillez remplir tous les champs obligatoires"); return; } setLoading(true); + // Ensure we're using the selected calendar ID const eventData = { - ...eventForm, - id: selectedEvent?.id, - calendarId: selectedCalendarId, - userId, - isAllDay: eventForm.allDay + title: eventForm.title, + description: eventForm.description, + start: new Date(eventForm.start).toISOString(), + end: new Date(eventForm.end).toISOString(), + allDay: eventForm.allDay, + location: eventForm.location, + calendarId: selectedCalendarId, // Use selectedCalendarId directly + userId }; + console.log("Submitting event with data:", eventData); - console.log("Available calendars:", calendars); + console.log("Available calendars:", calendars.map(c => ({ id: c.id, name: c.name }))); console.log("Selected calendar ID:", selectedCalendarId); const response = await fetch("/api/events", { @@ -688,8 +716,6 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend throw new Error(responseData.error || "Failed to save event"); } - console.log("Event saved successfully:", responseData); - // Reset form and close modal first setIsEventModalOpen(false); setEventForm({