calendar 5
This commit is contained in:
parent
c4aad6b803
commit
b852bada2b
@ -50,15 +50,25 @@ export async function GET(req: Request) {
|
|||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
try {
|
try {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
if (!session) {
|
if (!session?.user) {
|
||||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
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({
|
const event = await prisma.event.create({
|
||||||
data: {
|
data: {
|
||||||
...body,
|
title,
|
||||||
userId: session.user.id,
|
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) {
|
} catch (error) {
|
||||||
console.error("Error creating event:", error);
|
console.error("Error creating event:", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Error creating event" },
|
{ error: "Erreur lors de la création de l'événement" },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -88,22 +88,19 @@ export function CalendarClient({ initialCalendars, userId }: CalendarClientProps
|
|||||||
const handleEventSubmit = async () => {
|
const handleEventSubmit = async () => {
|
||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const method = selectedEvent ? "PUT" : "POST";
|
const response = await fetch("/api/calendar", {
|
||||||
const url = selectedEvent
|
method: "POST",
|
||||||
? `/api/calendar?id=${selectedEvent.id}`
|
|
||||||
: "/api/calendar";
|
|
||||||
|
|
||||||
const response = await fetch(url, {
|
|
||||||
method,
|
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
...eventForm,
|
title: eventForm.title,
|
||||||
id: selectedEvent?.id,
|
description: eventForm.description,
|
||||||
start: new Date(eventForm.start),
|
start: eventForm.start,
|
||||||
end: new Date(eventForm.end),
|
end: eventForm.end,
|
||||||
userId,
|
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");
|
throw new Error("Erreur lors de la sauvegarde de l'événement");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh calendar data
|
const newEvent = await response.json();
|
||||||
const eventsResponse = await fetch("/api/calendar");
|
// Update events state with the new event
|
||||||
const updatedEvents = await eventsResponse.json();
|
setCalendars((prev) => prev.map(cal => ({
|
||||||
setCalendars(calendars.map(cal => ({
|
|
||||||
...cal,
|
...cal,
|
||||||
events: updatedEvents.filter((event: Event) => event.calendarId === cal.id)
|
events: [...cal.events, newEvent]
|
||||||
})));
|
})));
|
||||||
|
|
||||||
setIsEventModalOpen(false);
|
setIsEventModalOpen(false);
|
||||||
setSelectedEvent(null);
|
|
||||||
setEventForm({
|
setEventForm({
|
||||||
title: "",
|
title: "",
|
||||||
description: null,
|
description: "",
|
||||||
start: "",
|
start: "",
|
||||||
end: "",
|
end: "",
|
||||||
allDay: false,
|
allDay: false,
|
||||||
location: null,
|
location: "",
|
||||||
|
calendarId: calendars[0]?.id || "",
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError((error as Error).message);
|
console.error("Error saving event:", error);
|
||||||
|
setError(error instanceof Error ? error.message : "Une erreur est survenue");
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
@ -176,6 +172,11 @@ export function CalendarClient({ initialCalendars, userId }: CalendarClientProps
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<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 */}
|
{/* Calendar filters and options */}
|
||||||
<div className="flex flex-wrap justify-between items-center gap-4 mb-4">
|
<div className="flex flex-wrap justify-between items-center gap-4 mb-4">
|
||||||
<div className="flex flex-wrap gap-2">
|
<div className="flex flex-wrap gap-2">
|
||||||
@ -225,12 +226,6 @@ export function CalendarClient({ initialCalendars, userId }: CalendarClientProps
|
|||||||
|
|
||||||
{/* Calendar display */}
|
{/* Calendar display */}
|
||||||
<Card className="p-4">
|
<Card className="p-4">
|
||||||
{error && (
|
|
||||||
<div className="p-4 mb-4 text-red-500 bg-red-50 rounded-md">
|
|
||||||
Erreur: {error}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{loading ? (
|
{loading ? (
|
||||||
<div className="h-96 flex items-center justify-center">
|
<div className="h-96 flex items-center justify-center">
|
||||||
<Loader2 className="h-8 w-8 animate-spin text-primary" />
|
<Loader2 className="h-8 w-8 animate-spin text-primary" />
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
version: '3.8'
|
version: '3.8'
|
||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
image: postgres:15
|
image: postgres:15-alpine
|
||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
- POSTGRES_USER=postgres
|
POSTGRES_USER: postgres
|
||||||
- POSTGRES_PASSWORD=postgres
|
POSTGRES_PASSWORD: postgres
|
||||||
- POSTGRES_DB=calendar_db
|
POSTGRES_DB: calendar_db
|
||||||
ports:
|
ports:
|
||||||
- '5432:5432'
|
- "5432:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- db:/var/lib/postgresql/data
|
- db:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
// front/lib/prisma.ts
|
// front/lib/prisma.ts
|
||||||
import { PrismaClient } from '@prisma/client'
|
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 =
|
export const prisma =
|
||||||
globalForPrisma.prisma ||
|
globalForPrisma.prisma ||
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user