Neah version calendar fix 3 debuger ???? ?

This commit is contained in:
alma 2025-04-16 22:07:28 +02:00
parent cb481a4f3f
commit 8d91a350c8
2 changed files with 91 additions and 96 deletions

View File

@ -1,5 +1,5 @@
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { authOptions } from "@/lib/auth";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { CalendarClient } from "@/components/calendar/calendar-client"; import { CalendarClient } from "@/components/calendar/calendar-client";
@ -110,6 +110,11 @@ export default async function CalendarPage() {
<CalendarClient <CalendarClient
initialCalendars={calendars} initialCalendars={calendars}
userId={session.user.id} userId={session.user.id}
userProfile={{
name: session.user.name || '',
email: session.user.email || '',
avatar: session.user.image || undefined
}}
/> />
</div> </div>
); );

View File

@ -75,8 +75,8 @@ interface CalendarClientProps {
interface EventFormData { interface EventFormData {
title: string; title: string;
description: string | null; description: string | null;
start: string; start: Date | null;
end: string; end: Date | null;
allDay: boolean; allDay: boolean;
location: string | null; location: string | null;
calendarId?: string; calendarId?: string;
@ -448,8 +448,8 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
const [eventForm, setEventForm] = useState<EventFormData>({ const [eventForm, setEventForm] = useState<EventFormData>({
title: "", title: "",
description: null, description: null,
start: "", start: null,
end: "", end: null,
allDay: false, allDay: false,
location: null, location: null,
calendarId: selectedCalendarId calendarId: selectedCalendarId
@ -466,6 +466,8 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
const [visibleCalendarIds, setVisibleCalendarIds] = useState<string[]>([]); const [visibleCalendarIds, setVisibleCalendarIds] = useState<string[]>([]);
const [selectedDate, setSelectedDate] = useState<Date | null>(null);
// Update useEffect to initialize visible calendars and fetch events // Update useEffect to initialize visible calendars and fetch events
useEffect(() => { useEffect(() => {
if (calendars.length > 0) { if (calendars.length > 0) {
@ -637,8 +639,8 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
setEventForm({ setEventForm({
title: "", title: "",
description: null, description: null,
start: startDate.toISOString(), start: startDate,
end: endDate.toISOString(), end: endDate,
allDay: selectInfo.allDay, allDay: selectInfo.allDay,
location: null, location: null,
calendarId: firstCalendar.id calendarId: firstCalendar.id
@ -647,8 +649,8 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
setEventForm({ setEventForm({
title: "", title: "",
description: null, description: null,
start: startDate.toISOString(), start: startDate,
end: endDate.toISOString(), end: endDate,
allDay: selectInfo.allDay, allDay: selectInfo.allDay,
location: null, location: null,
calendarId: selectedCalendarId calendarId: selectedCalendarId
@ -667,8 +669,8 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
setEventForm({ setEventForm({
title: event.title, title: event.title,
description: event.extendedProps.description, description: event.extendedProps.description,
start: startDate.toISOString().slice(0, 16), start: startDate,
end: endDate.toISOString().slice(0, 16), end: endDate,
allDay: event.isAllDay, allDay: event.isAllDay,
location: event.extendedProps.location, location: event.extendedProps.location,
calendarId: event.extendedProps.calendarId, calendarId: event.extendedProps.calendarId,
@ -676,79 +678,47 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
setIsEventModalOpen(true); setIsEventModalOpen(true);
}; };
const handleEventSubmit = async () => { const handleEventSubmit = async (event: React.MouseEvent<HTMLButtonElement>) => {
try { try {
// Validate required fields including calendar setLoading(true);
if (!eventForm.title || !eventForm.start || !eventForm.end || !eventForm.calendarId) { if (!eventForm.start || !eventForm.end) {
console.log("Form validation failed:", { throw new Error('Start and end dates are required');
title: eventForm.title,
start: eventForm.start,
end: eventForm.end,
calendarId: eventForm.calendarId
});
setError("Veuillez remplir tous les champs obligatoires et sélectionner un calendrier");
return;
} }
setLoading(true);
const eventData = { const eventData = {
...eventForm, ...eventForm,
start: new Date(eventForm.start).toISOString(), start: eventForm.start.toISOString(),
end: new Date(eventForm.end).toISOString(), end: eventForm.end.toISOString(),
userId, userId,
...(selectedEvent ? { id: selectedEvent.id } : {}), // Include ID for updates
allDay: eventForm.allDay // Use allDay instead of isAllDay
}; };
console.log("Submitting event with data:", eventData); const response = await fetch('/api/events', {
method: 'POST',
const response = await fetch("/api/events", {
method: selectedEvent ? "PUT" : "POST",
headers: { headers: {
"Content-Type": "application/json", 'Content-Type': 'application/json',
}, },
body: JSON.stringify(eventData), body: JSON.stringify(eventData),
credentials: 'include'
}); });
const responseData = await response.json();
console.log("Response from server:", responseData);
if (!response.ok) { if (!response.ok) {
console.error("Error response:", responseData); throw new Error('Failed to save event');
throw new Error(responseData.error || "Failed to save event");
} }
// Reset form and close modal first const newEvent = await response.json();
setIsEventModalOpen(false); setCalendars(prev => prev.map(cal => {
setEventForm({ if (cal.id === newEvent.calendarId) {
title: "",
description: null,
start: "",
end: "",
allDay: false,
location: null,
calendarId: selectedCalendarId
});
setSelectedEvent(null);
setError(null);
// Update calendars state with the new event
const updatedCalendars = calendars.map(cal => {
if (cal.id === eventData.calendarId) {
return { return {
...cal, ...cal,
events: [...cal.events, responseData] events: [...cal.events, newEvent],
}; };
} }
return cal; return cal;
}); }));
setCalendars(updatedCalendars); setError(null);
} catch (err) {
// Fetch fresh data to ensure all calendars are up to date setError('Failed to save event');
await fetchCalendars(); console.error('Error saving event:', err);
} catch (error) {
console.error("Error saving event:", error);
setError(error instanceof Error ? error.message : "Failed to save event");
} finally { } finally {
setLoading(false); setLoading(false);
} }
@ -785,8 +755,8 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
setEventForm({ setEventForm({
title: "", title: "",
description: null, description: null,
start: "", start: null,
end: "", end: null,
allDay: false, allDay: false,
location: null, location: null,
calendarId: selectedCalendarId calendarId: selectedCalendarId
@ -867,42 +837,42 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
const handleStartDateChange = (date: Date | null) => { const handleStartDateChange = (date: Date | null) => {
if (!date) return; if (!date) return;
const endDate = getDateFromString(eventForm.end); const endDate = getDateFromString(eventForm.end?.toISOString() || "");
if (date > endDate) { if (date > endDate) {
// If start date is after end date, set end date to start date + 1 hour // If start date is after end date, set end date to start date + 1 hour
const newEndDate = new Date(date); const newEndDate = new Date(date);
newEndDate.setHours(date.getHours() + 1); newEndDate.setHours(date.getHours() + 1);
setEventForm({ setEventForm(prev => ({
...eventForm, ...prev,
start: date.toISOString(), start: date,
end: newEndDate.toISOString(), end: newEndDate,
}); }));
} else { } else {
setEventForm({ setEventForm(prev => ({
...eventForm, ...prev,
start: date.toISOString(), start: date,
}); }));
} }
}; };
const handleEndDateChange = (date: Date | null) => { const handleEndDateChange = (date: Date | null) => {
if (!date) return; if (!date) return;
const startDate = getDateFromString(eventForm.start); const startDate = getDateFromString(eventForm.start?.toISOString() || "");
if (date < startDate) { if (date < startDate) {
// If end date is before start date, set start date to end date - 1 hour // If end date is before start date, set start date to end date - 1 hour
const newStartDate = new Date(date); const newStartDate = new Date(date);
newStartDate.setHours(date.getHours() - 1); newStartDate.setHours(date.getHours() - 1);
setEventForm({ setEventForm(prev => ({
...eventForm, ...prev,
start: newStartDate.toISOString(), start: newStartDate,
end: date.toISOString(), end: date,
}); }));
} else { } else {
setEventForm({ setEventForm(prev => ({
...eventForm, ...prev,
end: date.toISOString(), end: date,
}); }));
} }
}; };
@ -1160,8 +1130,8 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
setEventForm({ setEventForm({
title: "", title: "",
description: null, description: null,
start: "", start: null,
end: "", end: null,
allDay: false, allDay: false,
location: null, location: null,
calendarId: selectedCalendarId || calendars[0]?.id calendarId: selectedCalendarId || calendars[0]?.id
@ -1236,8 +1206,13 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
<div className="flex gap-2"> <div className="flex gap-2">
<div className="flex-1"> <div className="flex-1">
<DatePicker <DatePicker
selected={getDateFromString(eventForm.start)} selected={eventForm.start || undefined}
onChange={handleStartDateChange} onChange={(date: Date | null) => {
setEventForm(prev => ({
...prev,
start: date
}));
}}
dateFormat="dd/MM/yyyy" dateFormat="dd/MM/yyyy"
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"
@ -1246,8 +1221,13 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
/> />
</div> </div>
<DatePicker <DatePicker
selected={getDateFromString(eventForm.start)} selected={eventForm.start || undefined}
onChange={handleStartDateChange} onChange={(date: Date | null) => {
setEventForm(prev => ({
...prev,
start: date
}));
}}
showTimeSelect showTimeSelect
showTimeSelectOnly showTimeSelectOnly
timeIntervals={15} timeIntervals={15}
@ -1264,19 +1244,29 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
<div className="flex gap-2"> <div className="flex gap-2">
<div className="flex-1"> <div className="flex-1">
<DatePicker <DatePicker
selected={getDateFromString(eventForm.end)} selected={eventForm.end || undefined}
onChange={handleEndDateChange} onChange={(date: Date | null) => {
setEventForm(prev => ({
...prev,
end: date
}));
}}
dateFormat="dd/MM/yyyy" dateFormat="dd/MM/yyyy"
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="Date" placeholderText="Date"
customInput={<Input />} customInput={<Input />}
minDate={getDateFromString(eventForm.start)} minDate={eventForm.start || undefined}
/> />
</div> </div>
<DatePicker <DatePicker
selected={getDateFromString(eventForm.end)} selected={eventForm.end || undefined}
onChange={handleEndDateChange} onChange={(date: Date | null) => {
setEventForm(prev => ({
...prev,
end: date
}));
}}
showTimeSelect showTimeSelect
showTimeSelectOnly showTimeSelectOnly
timeIntervals={15} timeIntervals={15}