calendar 20

This commit is contained in:
Alma 2025-04-13 15:39:35 +02:00
parent 24f6751517
commit 68292d9076
2 changed files with 81 additions and 73 deletions

View File

@ -6,7 +6,7 @@ import { prisma } from "@/lib/prisma";
export async function POST(req: NextRequest) { export async function POST(req: NextRequest) {
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);
if (!session?.user?.username) { if (!session?.user?.id) {
return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
} }
@ -25,7 +25,7 @@ export async function POST(req: NextRequest) {
const calendar = await prisma.calendar.findFirst({ const calendar = await prisma.calendar.findFirst({
where: { where: {
id: calendarId, id: calendarId,
userId: session.user.username, userId: session.user.id,
}, },
}); });
@ -46,8 +46,8 @@ export async function POST(req: NextRequest) {
isAllDay: allDay || false, isAllDay: allDay || false,
location: location || null, location: location || null,
calendarId, calendarId,
userId: session.user.username, userId: session.user.id,
} as any, // Temporary type assertion to bypass the type error },
}); });
console.log("Created event:", event); console.log("Created event:", event);
@ -61,7 +61,7 @@ export async function POST(req: NextRequest) {
export async function PUT(req: NextRequest) { export async function PUT(req: NextRequest) {
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);
if (!session?.user?.username) { if (!session?.user?.id) {
return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
} }
@ -80,7 +80,7 @@ export async function PUT(req: NextRequest) {
const calendar = await prisma.calendar.findFirst({ const calendar = await prisma.calendar.findFirst({
where: { where: {
id: calendarId, id: calendarId,
userId: session.user.username, userId: session.user.id,
}, },
include: { include: {
events: { events: {
@ -107,11 +107,7 @@ export async function PUT(req: NextRequest) {
end: new Date(end), end: new Date(end),
isAllDay: allDay || false, isAllDay: allDay || false,
location, location,
calendar: { calendarId,
connect: {
id: calendarId
}
}
}, },
}); });

View File

@ -620,21 +620,20 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
const handleEventSubmit = async () => { const handleEventSubmit = async () => {
try { try {
setLoading(true); setLoading(true);
console.log("Submitting event:", { const eventData = {
...eventForm, ...eventForm,
id: selectedEvent?.id,
calendarId: selectedCalendarId, calendarId: selectedCalendarId,
userId, userId,
}); };
console.log("Submitting event:", eventData);
const response = await fetch("/api/events", { const response = await fetch("/api/events", {
method: selectedEvent ? "PUT" : "POST", method: selectedEvent ? "PUT" : "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify(eventData),
...eventForm,
calendarId: selectedCalendarId,
userId,
}),
}); });
if (!response.ok) { if (!response.ok) {
@ -648,11 +647,8 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
// Refresh calendars and update the view // Refresh calendars and update the view
await fetchCalendars(); await fetchCalendars();
if (calendarRef.current) {
const calendarApi = calendarRef.current.getApi();
calendarApi.refetchEvents();
}
// Reset form and close modal
setIsEventModalOpen(false); setIsEventModalOpen(false);
setEventForm({ setEventForm({
title: "", title: "",
@ -663,6 +659,13 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
location: null, location: null,
calendarId: selectedCalendarId calendarId: selectedCalendarId
}); });
setSelectedEvent(null);
// Force calendar refresh
if (calendarRef.current) {
const calendarApi = calendarRef.current.getApi();
calendarApi.refetchEvents();
}
} catch (error) { } catch (error) {
console.error("Error saving event:", error); console.error("Error saving event:", error);
setError(error instanceof Error ? error.message : "Failed to save event"); setError(error instanceof Error ? error.message : "Failed to save event");
@ -922,66 +925,75 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
/> />
</div> </div>
<div className="space-y-2">
<Label>Calendrier</Label>
<select
value={selectedCalendarId}
onChange={(e) => setSelectedCalendarId(e.target.value)}
className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary"
>
{calendars.map((cal) => (
<option key={cal.id} value={cal.id}>
{cal.name}
</option>
))}
</select>
</div>
<div className="grid grid-cols-2 gap-4"> <div className="grid grid-cols-2 gap-4">
<div className="space-y-2"> <div className="space-y-2">
<Label>Début</Label> <Label>Début</Label>
<div className="relative"> <div className="flex gap-2">
<div className="flex-1">
<DatePicker <DatePicker
selected={getDateFromString(eventForm.start)} selected={getDateFromString(eventForm.start)}
onChange={handleStartDateChange} onChange={handleStartDateChange}
showTimeSelect dateFormat="dd/MM/yyyy"
timeFormat="HH:mm"
timeIntervals={15}
timeCaption="Heure"
dateFormat="dd/MM/yyyy HH:mm"
locale="fr" locale="fr"
className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary" className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary"
placeholderText="Sélectionner la date et l'heure" placeholderText="Date"
customInput={<Input />} customInput={<Input />}
showTimeSelectOnly={false} />
inline={false} </div>
timeClassName={() => "text-primary"} <Input
popperModifiers={[ type="time"
{ value={eventForm.start ? new Date(eventForm.start).toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' }) : ''}
name: "preventOverflow", onChange={(e) => {
options: { const [hours, minutes] = e.target.value.split(':');
padding: 10 const date = getDateFromString(eventForm.start);
} date.setHours(parseInt(hours), parseInt(minutes));
} handleStartDateChange(date);
]} }}
popperPlacement="bottom-start" className="w-32"
/> />
</div> </div>
</div> </div>
<div className="space-y-2"> <div className="space-y-2">
<Label>Fin</Label> <Label>Fin</Label>
<div className="relative"> <div className="flex gap-2">
<div className="flex-1">
<DatePicker <DatePicker
selected={getDateFromString(eventForm.end)} selected={getDateFromString(eventForm.end)}
onChange={handleEndDateChange} onChange={handleEndDateChange}
showTimeSelect dateFormat="dd/MM/yyyy"
timeFormat="HH:mm"
timeIntervals={15}
timeCaption="Heure"
dateFormat="dd/MM/yyyy HH:mm"
locale="fr" locale="fr"
className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary" className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary"
placeholderText="Sélectionner la date et l'heure" placeholderText="Date"
minDate={getDateFromString(eventForm.start)}
customInput={<Input />} customInput={<Input />}
showTimeSelectOnly={false} minDate={getDateFromString(eventForm.start)}
inline={false} />
timeClassName={() => "text-primary"} </div>
popperModifiers={[ <Input
{ type="time"
name: "preventOverflow", value={eventForm.end ? new Date(eventForm.end).toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' }) : ''}
options: { onChange={(e) => {
padding: 10 const [hours, minutes] = e.target.value.split(':');
} const date = getDateFromString(eventForm.end);
} date.setHours(parseInt(hours), parseInt(minutes));
]} handleEndDateChange(date);
popperPlacement="bottom-start" }}
className="w-32"
/> />
</div> </div>
</div> </div>