diff --git a/app/api/events/route.ts b/app/api/events/route.ts index d3ffdf23..0e4255ea 100644 --- a/app/api/events/route.ts +++ b/app/api/events/route.ts @@ -6,7 +6,7 @@ import { prisma } from "@/lib/prisma"; export async function POST(req: NextRequest) { const session = await getServerSession(authOptions); - if (!session?.user?.username) { + if (!session?.user?.id) { return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); } @@ -25,7 +25,7 @@ export async function POST(req: NextRequest) { const calendar = await prisma.calendar.findFirst({ where: { id: calendarId, - userId: session.user.username, + userId: session.user.id, }, }); @@ -46,8 +46,8 @@ export async function POST(req: NextRequest) { isAllDay: allDay || false, location: location || null, calendarId, - userId: session.user.username, - } as any, // Temporary type assertion to bypass the type error + userId: session.user.id, + }, }); console.log("Created event:", event); @@ -61,7 +61,7 @@ export async function POST(req: NextRequest) { export async function PUT(req: NextRequest) { const session = await getServerSession(authOptions); - if (!session?.user?.username) { + if (!session?.user?.id) { return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); } @@ -80,7 +80,7 @@ export async function PUT(req: NextRequest) { const calendar = await prisma.calendar.findFirst({ where: { id: calendarId, - userId: session.user.username, + userId: session.user.id, }, include: { events: { @@ -107,11 +107,7 @@ export async function PUT(req: NextRequest) { end: new Date(end), isAllDay: allDay || false, location, - calendar: { - connect: { - id: calendarId - } - } + calendarId, }, }); diff --git a/components/calendar/calendar-client.tsx b/components/calendar/calendar-client.tsx index 40be9ac9..8e951cf8 100644 --- a/components/calendar/calendar-client.tsx +++ b/components/calendar/calendar-client.tsx @@ -620,21 +620,20 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend const handleEventSubmit = async () => { try { setLoading(true); - console.log("Submitting event:", { + const eventData = { ...eventForm, + id: selectedEvent?.id, calendarId: selectedCalendarId, userId, - }); + }; + console.log("Submitting event:", eventData); + const response = await fetch("/api/events", { method: selectedEvent ? "PUT" : "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ - ...eventForm, - calendarId: selectedCalendarId, - userId, - }), + body: JSON.stringify(eventData), }); if (!response.ok) { @@ -648,11 +647,8 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend // Refresh calendars and update the view await fetchCalendars(); - if (calendarRef.current) { - const calendarApi = calendarRef.current.getApi(); - calendarApi.refetchEvents(); - } + // Reset form and close modal setIsEventModalOpen(false); setEventForm({ title: "", @@ -663,6 +659,13 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend location: null, calendarId: selectedCalendarId }); + setSelectedEvent(null); + + // Force calendar refresh + if (calendarRef.current) { + const calendarApi = calendarRef.current.getApi(); + calendarApi.refetchEvents(); + } } catch (error) { console.error("Error saving event:", error); setError(error instanceof Error ? error.message : "Failed to save event"); @@ -922,66 +925,75 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend /> +