calendar 12

This commit is contained in:
Alma 2025-04-13 14:05:35 +02:00
parent c88091a1dc
commit 04d266614c

View File

@ -412,6 +412,7 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
end: "", end: "",
allDay: false, allDay: false,
location: null, location: null,
calendarId: selectedCalendarId
}); });
const [selectedEventPreview, setSelectedEventPreview] = useState<Event | null>(null); const [selectedEventPreview, setSelectedEventPreview] = useState<Event | null>(null);
@ -439,6 +440,31 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
const calendarRef = useRef<any>(null); const calendarRef = useRef<any>(null);
const fetchCalendars = async () => {
try {
setLoading(true);
const response = await fetch("/api/calendars");
if (!response.ok) throw new Error("Failed to fetch calendars");
const data = await response.json();
setCalendars(data.map((cal: Calendar & { events: Event[] }) => ({
...cal,
events: cal.events || []
})));
if (data.length > 0 && !selectedCalendarId) {
setSelectedCalendarId(data[0].id);
}
} catch (error) {
console.error("Error fetching calendars:", error);
setError(error instanceof Error ? error.message : "Failed to fetch calendars");
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchCalendars();
}, []);
const handleCalendarSave = async (calendarData: Partial<Calendar>) => { const handleCalendarSave = async (calendarData: Partial<Calendar>) => {
try { try {
setLoading(true); setLoading(true);
@ -454,21 +480,14 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
}); });
if (!response.ok) { if (!response.ok) {
throw new Error("Erreur lors de la sauvegarde du calendrier"); throw new Error("Failed to save calendar");
} }
const updatedCalendar = await response.json(); await fetchCalendars();
if (calendarData.id) {
setCalendars(calendars.map(cal =>
cal.id === calendarData.id ? { ...cal, ...updatedCalendar } : cal
));
} else {
setCalendars([...calendars, updatedCalendar]);
}
setIsCalendarModalOpen(false); setIsCalendarModalOpen(false);
} catch (error) { } catch (error) {
console.error("Error saving calendar:", error); console.error("Error saving calendar:", error);
setError(error instanceof Error ? error.message : "Une erreur est survenue"); setError(error instanceof Error ? error.message : "Failed to save calendar");
} finally { } finally {
setLoading(false); setLoading(false);
} }
@ -511,45 +530,36 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
const handleEventSubmit = async () => { const handleEventSubmit = async () => {
try { try {
setLoading(true); setLoading(true);
const response = await fetch("/api/calendar", { const response = await fetch("/api/events", {
method: "POST", method: selectedEvent ? "PUT" : "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
title: eventForm.title, ...eventForm,
description: eventForm.description, calendarId: selectedCalendarId,
start: eventForm.start, userId,
end: eventForm.end,
isAllDay: eventForm.allDay,
location: eventForm.location,
calendarId: eventForm.calendarId,
}), }),
}); });
if (!response.ok) { if (!response.ok) {
throw new Error("Erreur lors de la sauvegarde de l'événement"); throw new Error("Failed to save event");
} }
const newEvent = await response.json(); await fetchCalendars();
// Update events state with the new event
setCalendars((prev) => prev.map(cal => ({
...cal,
events: [...cal.events, newEvent]
})));
setIsEventModalOpen(false); setIsEventModalOpen(false);
setEventForm({ setEventForm({
title: "", title: "",
description: "", description: null,
start: "", start: "",
end: "", end: "",
allDay: false, allDay: false,
location: "", location: null,
calendarId: calendars[0]?.id || "", calendarId: selectedCalendarId
}); });
} catch (error) { } catch (error) {
console.error("Error saving event:", error); console.error("Error saving event:", error);
setError(error instanceof Error ? error.message : "Une erreur est survenue"); setError(error instanceof Error ? error.message : "Failed to save event");
} finally { } finally {
setLoading(false); setLoading(false);
} }
@ -560,26 +570,20 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
try { try {
setLoading(true); setLoading(true);
const response = await fetch(`/api/calendar?id=${selectedEvent.id}`, { const response = await fetch(`/api/events/${selectedEvent.id}`, {
method: "DELETE", method: "DELETE",
}); });
if (!response.ok) { if (!response.ok) {
throw new Error("Erreur lors de la suppression de l'événement"); throw new Error("Failed to delete event");
} }
// Refresh calendar data await fetchCalendars();
const eventsResponse = await fetch("/api/calendar");
const updatedEvents = await eventsResponse.json();
setCalendars(calendars.map(cal => ({
...cal,
events: updatedEvents.filter((event: Event) => event.calendarId === cal.id)
})));
setIsEventModalOpen(false); setIsEventModalOpen(false);
setSelectedEvent(null); setSelectedEvent(null);
} catch (error) { } catch (error) {
setError((error as Error).message); console.error("Error deleting event:", error);
setError(error instanceof Error ? error.message : "Failed to delete event");
} finally { } finally {
setLoading(false); setLoading(false);
} }
@ -612,6 +616,15 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
<Button <Button
onClick={() => { onClick={() => {
setSelectedEvent(null); setSelectedEvent(null);
setEventForm({
title: "",
description: null,
start: "",
end: "",
allDay: false,
location: null,
calendarId: selectedCalendarId
});
setIsEventModalOpen(true); setIsEventModalOpen(true);
}} }}
className="bg-primary hover:bg-primary/90 text-white" className="bg-primary hover:bg-primary/90 text-white"