diff --git a/components/calendar/calendar-client.tsx b/components/calendar/calendar-client.tsx index 790f3f8..7e29fd5 100644 --- a/components/calendar/calendar-client.tsx +++ b/components/calendar/calendar-client.tsx @@ -994,32 +994,26 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend const endDate = new Date(startDate); endDate.setHours(startDate.getHours() + 1); - // If no calendar is selected, use the first available calendar - if (!selectedCalendarId && calendars.length > 0) { - const firstCalendar = calendars[0]; - setSelectedCalendarId(firstCalendar.id); - - setEventForm({ - title: "", - description: null, - start: startDate.toISOString(), - end: endDate.toISOString(), - allDay: false, - location: null, - calendarId: firstCalendar.id - }); - } else { - setEventForm({ - title: "", - description: null, - start: startDate.toISOString(), - end: endDate.toISOString(), - allDay: false, - location: null, - calendarId: selectedCalendarId - }); + // Get available calendars and use the first one if needed + const availableCalendarsList = getAvailableCalendars(); + const calendarToUse = selectedCalendarId && availableCalendarsList.some(cal => cal.id === selectedCalendarId) + ? selectedCalendarId + : (availableCalendarsList[0]?.id || calendars[0]?.id); + + if (calendarToUse && calendarToUse !== selectedCalendarId) { + setSelectedCalendarId(calendarToUse); } + setEventForm({ + title: "", + description: null, + start: startDate.toISOString(), + end: endDate.toISOString(), + allDay: false, + location: null, + calendarId: calendarToUse + }); + setIsEventModalOpen(true); lastClickDateRef.current = null; } else { @@ -1050,33 +1044,26 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend selectDate.setHours(0, 0, 0, 0); setSelectedDate(selectDate); - // If no calendar is selected, use the first available calendar - if (!selectedCalendarId && calendars.length > 0) { - const firstCalendar = calendars[0]; - console.log("No calendar selected, selecting first calendar:", firstCalendar); - setSelectedCalendarId(firstCalendar.id); - - setEventForm({ - title: "", - description: null, - start: startDate.toISOString(), - end: endDate.toISOString(), - allDay: selectInfo.allDay, - location: null, - calendarId: firstCalendar.id - }); - } else { - setEventForm({ - title: "", - description: null, - start: startDate.toISOString(), - end: endDate.toISOString(), - allDay: selectInfo.allDay, - location: null, - calendarId: selectedCalendarId - }); + // Get available calendars and use the first one if needed + const availableCalendarsList = getAvailableCalendars(); + const calendarToUse = selectedCalendarId && availableCalendarsList.some(cal => cal.id === selectedCalendarId) + ? selectedCalendarId + : (availableCalendarsList[0]?.id || calendars[0]?.id); + + if (calendarToUse && calendarToUse !== selectedCalendarId) { + setSelectedCalendarId(calendarToUse); } + setEventForm({ + title: "", + description: null, + start: startDate.toISOString(), + end: endDate.toISOString(), + allDay: selectInfo.allDay || false, + location: null, + calendarId: calendarToUse + }); + setIsEventModalOpen(true); }; @@ -1085,6 +1072,15 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend const startDate = new Date(event.start); const endDate = new Date(event.end || event.start); + const eventCalendarId = event.extendedProps.calendarId; + const availableCalendars = getAvailableCalendars(); + + // Check if the event's calendar is still available for editing + const canEditEventCalendar = availableCalendars.some(cal => cal.id === eventCalendarId); + + // If not available, use the first available calendar instead + const calendarIdToUse = canEditEventCalendar ? eventCalendarId : (availableCalendars[0]?.id || eventCalendarId); + setSelectedEvent(event.extendedProps.originalEvent); setEventForm({ title: event.title, @@ -1093,7 +1089,7 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend end: endDate.toISOString().slice(0, 16), allDay: event.isAllDay, location: event.extendedProps.location, - calendarId: event.extendedProps.calendarId, + calendarId: calendarIdToUse, }); setIsEventModalOpen(true); }; @@ -1248,6 +1244,59 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend return calendar.name; }; + // Check if user can create/edit events in a mission calendar + const canEditMissionCalendar = (calendar: CalendarWithMission): boolean => { + // If not a mission calendar, allow + if (!calendar.missionId || !calendar.mission) { + return true; + } + + const mission = calendar.mission; + + // Check if user is the creator + if (mission.creatorId === userId) { + return true; + } + + // Check if user has one of the guardian roles + const guardianRoles = ['gardien-temps', 'gardien-parole', 'gardien-memoire']; + const userMissionRole = mission.missionUsers?.find( + mu => mu.userId === userId && guardianRoles.includes(mu.role) + ); + + return !!userMissionRole; + }; + + // Get available calendars for event creation/editing + const getAvailableCalendars = (): CalendarWithMission[] => { + return calendars.filter(cal => { + const calWithMission = cal as CalendarWithMission; + + // Always allow "Mon Calendrier" + if (cal.name === "Mon Calendrier") { + return true; + } + + // Always allow Microsoft synced calendars + if (calWithMission.syncConfig?.syncEnabled && calWithMission.syncConfig?.mailCredential) { + return true; + } + + // Always allow group calendars + if (cal.name?.startsWith("Groupe:")) { + return true; + } + + // For mission calendars, check permissions + if (cal.name?.startsWith("Mission:")) { + return canEditMissionCalendar(calWithMission); + } + + // Allow other calendars by default + return true; + }); + }; + // Update CalendarSelector to handle visibility - displayed as a left column const CalendarSelector = () => (