calendar 5

This commit is contained in:
Alma 2025-04-13 13:05:02 +02:00
parent c4aad6b803
commit b852bada2b
4 changed files with 47 additions and 40 deletions

View File

@ -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 }
);
}

View File

@ -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 (
<div className="space-y-4">
{error && (
<div className="p-4 mb-4 text-red-500 bg-red-50 rounded-md">
{error}
</div>
)}
{/* Calendar filters and options */}
<div className="flex flex-wrap justify-between items-center gap-4 mb-4">
<div className="flex flex-wrap gap-2">
@ -225,12 +226,6 @@ export function CalendarClient({ initialCalendars, userId }: CalendarClientProps
{/* Calendar display */}
<Card className="p-4">
{error && (
<div className="p-4 mb-4 text-red-500 bg-red-50 rounded-md">
Erreur: {error}
</div>
)}
{loading ? (
<div className="h-96 flex items-center justify-center">
<Loader2 className="h-8 w-8 animate-spin text-primary" />

View File

@ -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

View File

@ -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 ||