From b852bada2b5df02d79d502c2e808de11fd79175f Mon Sep 17 00:00:00 2001 From: Alma Date: Sun, 13 Apr 2025 13:05:02 +0200 Subject: [PATCH] calendar 5 --- app/api/calendar/route.ts | 22 ++++++++--- components/calendar/calendar-client.tsx | 51 +++++++++++-------------- docker-compose.yml | 10 ++--- lib/prisma.ts | 4 +- 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/app/api/calendar/route.ts b/app/api/calendar/route.ts index 360623d2..94e90e68 100644 --- a/app/api/calendar/route.ts +++ b/app/api/calendar/route.ts @@ -50,15 +50,25 @@ export async function GET(req: Request) { export async function POST(req: Request) { try { const session = await getServerSession(authOptions); - if (!session) { - return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + if (!session?.user) { + return NextResponse.json( + { error: "Non autorisé" }, + { status: 401 } + ); } - const body = await req.json(); + const data = await req.json(); + const { title, description, start, end, location, calendarId } = data; + const event = await prisma.event.create({ data: { - ...body, - userId: session.user.id, + title, + description, + start: new Date(start), + end: new Date(end), + isAllDay: data.allDay || false, + location: location || null, + calendarId, }, }); @@ -66,7 +76,7 @@ export async function POST(req: Request) { } catch (error) { console.error("Error creating event:", error); return NextResponse.json( - { error: "Error creating event" }, + { error: "Erreur lors de la création de l'événement" }, { status: 500 } ); } diff --git a/components/calendar/calendar-client.tsx b/components/calendar/calendar-client.tsx index 3c2694dc..cad7a3a0 100644 --- a/components/calendar/calendar-client.tsx +++ b/components/calendar/calendar-client.tsx @@ -88,22 +88,19 @@ export function CalendarClient({ initialCalendars, userId }: CalendarClientProps const handleEventSubmit = async () => { try { setLoading(true); - const method = selectedEvent ? "PUT" : "POST"; - const url = selectedEvent - ? `/api/calendar?id=${selectedEvent.id}` - : "/api/calendar"; - - const response = await fetch(url, { - method, + const response = await fetch("/api/calendar", { + method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ - ...eventForm, - id: selectedEvent?.id, - start: new Date(eventForm.start), - end: new Date(eventForm.end), - userId, + title: eventForm.title, + description: eventForm.description, + start: eventForm.start, + end: eventForm.end, + isAllDay: eventForm.allDay, + location: eventForm.location, + calendarId: eventForm.calendarId, }), }); @@ -111,26 +108,25 @@ export function CalendarClient({ initialCalendars, userId }: CalendarClientProps throw new Error("Erreur lors de la sauvegarde de l'événement"); } - // Refresh calendar data - const eventsResponse = await fetch("/api/calendar"); - const updatedEvents = await eventsResponse.json(); - setCalendars(calendars.map(cal => ({ + const newEvent = await response.json(); + // Update events state with the new event + setCalendars((prev) => prev.map(cal => ({ ...cal, - events: updatedEvents.filter((event: Event) => event.calendarId === cal.id) + events: [...cal.events, newEvent] }))); - setIsEventModalOpen(false); - setSelectedEvent(null); setEventForm({ title: "", - description: null, + description: "", start: "", end: "", allDay: false, - location: null, + location: "", + calendarId: calendars[0]?.id || "", }); } catch (error) { - setError((error as Error).message); + console.error("Error saving event:", error); + setError(error instanceof Error ? error.message : "Une erreur est survenue"); } finally { setLoading(false); } @@ -176,6 +172,11 @@ export function CalendarClient({ initialCalendars, userId }: CalendarClientProps return (
+ {error && ( +
+ {error} +
+ )} {/* Calendar filters and options */}
@@ -225,12 +226,6 @@ export function CalendarClient({ initialCalendars, userId }: CalendarClientProps {/* Calendar display */} - {error && ( -
- Erreur: {error} -
- )} - {loading ? (
diff --git a/docker-compose.yml b/docker-compose.yml index 3d94cfc4..0280c8fe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,14 +1,14 @@ version: '3.8' services: db: - image: postgres:15 + image: postgres:15-alpine restart: always environment: - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=postgres - - POSTGRES_DB=calendar_db + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: calendar_db ports: - - '5432:5432' + - "5432:5432" volumes: - db:/var/lib/postgresql/data diff --git a/lib/prisma.ts b/lib/prisma.ts index f78fd34c..23adc83d 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -1,7 +1,9 @@ // front/lib/prisma.ts import { PrismaClient } from '@prisma/client' -const globalForPrisma = global as unknown as { prisma: PrismaClient } +const globalForPrisma = globalThis as unknown as { + prisma: PrismaClient | undefined; +} export const prisma = globalForPrisma.prisma ||