From d2ab27c7aa0745b6d7ba10679314a44579285867 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Sun, 17 Mar 2024 20:50:58 +0000 Subject: [PATCH] Support all-day events in day view --- islands/calendar/MainCalendar.tsx | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/islands/calendar/MainCalendar.tsx b/islands/calendar/MainCalendar.tsx index fc65416..f96b32c 100644 --- a/islands/calendar/MainCalendar.tsx +++ b/islands/calendar/MainCalendar.tsx @@ -330,6 +330,7 @@ export default function MainCalendar({ initialCalendars, initialCalendarEvents, // TODO: Send in / consider user timezone const weeks = view === 'month' ? getWeeksForMonth(new Date(startDate)) : []; + const hours: { date: Date; isCurrentHour: boolean }[] = view === 'day' ? Array.from({ length: 24 }).map((_, index) => { const hourNumber = index; @@ -348,6 +349,50 @@ export default function MainCalendar({ initialCalendars, initialCalendarEvents, }) : []; + const allDayEvents: CalendarEvent[] = view === 'day' + ? calendarEvents.value.filter((calendarEvent) => { + if (!calendarEvent.is_all_day) { + return false; + } + + const startDayDate = new Date(startDate); + const endDayDate = new Date(startDate); + endDayDate.setHours(23); + endDayDate.setMinutes(59); + endDayDate.setSeconds(59); + endDayDate.setMilliseconds(999); + + const eventStartDate = new Date(calendarEvent.start_date); + const eventEndDate = new Date(calendarEvent.end_date); + + // Event starts and ends on this day + if (eventStartDate >= startDayDate && eventEndDate <= endDayDate) { + return true; + } + + // Event starts before and ends after this day + if (eventStartDate <= startDayDate && eventEndDate >= endDayDate) { + return true; + } + + // Event starts on and ends after this day + if ( + eventStartDate >= startDayDate && eventStartDate <= endDayDate && eventEndDate >= endDayDate + ) { + return true; + } + + // Event starts before and ends on this day + if ( + eventStartDate <= startDayDate && eventEndDate >= startDayDate && eventEndDate <= endDayDate + ) { + return true; + } + + return false; + }) + : []; + // TODO: days with hours return ( @@ -540,6 +585,35 @@ export default function MainCalendar({ initialCalendars, initialCalendarEvents,
+ {allDayEvents.length > 0 + ? ( +
+ +
    + {allDayEvents.map((calendarEvent) => ( +
  1. + calendar.id === calendarEvent.calendar_id) + ?.color || 'bg-gray-700' + }`} + onClick={() => openEvent.value = calendarEvent} + > +

    + {calendarEvent.title} +

    +
    +
  2. + ))} +
+
+ ) + : null} {hours.map((hour, hourIndex) => { const shortIsoDate = hour.date.toISOString().substring(0, 10); @@ -554,6 +628,10 @@ export default function MainCalendar({ initialCalendars, initialCalendarEvents, const isLastHour = hourIndex === 23; const hourEvents = calendarEvents.value.filter((calendarEvent) => { + if (calendarEvent.is_all_day) { + return false; + } + const eventStartDate = new Date(calendarEvent.start_date); const eventEndDate = new Date(calendarEvent.end_date); eventEndDate.setSeconds(eventEndDate.getSeconds() - 1); // Take one second back so events don't bleed into the next hour