diff --git a/src/components/Schedule/index.tsx b/src/components/Schedule/index.tsx index f4e54cd..756bfe2 100644 --- a/src/components/Schedule/index.tsx +++ b/src/components/Schedule/index.tsx @@ -71,25 +71,35 @@ const ScheduleBase = ({ events, title, description }: ScheduleBaseProps) => { setSelectedDate={setSelectedDate} /> - + <> - - Events for {selectedDate?.format('MMMM D, YYYY')}: - + + Events for {selectedDate?.format('MMMM D, YYYY')}: + {eventsForSelectedDate.length > 0 ? ( @@ -154,7 +164,7 @@ const ScheduleBase = ({ events, title, description }: ScheduleBaseProps) => { No events for this date. )} - + {/* Event Details Modal */} { +const formatDate = (dateString: string): string => { const date = new Date(dateString); return date .toISOString() @@ -6,12 +6,20 @@ const formatDate = (dateString: string) => { .replace(/\.\d{3}/, ''); }; -const createICSContent = (event: DonationEvent) => { +const downloadICSFile = (fileName: string, content: string): void => { + const blob = new Blob([content], { type: 'text/calendar;charset=utf-8' }); + const link = document.createElement('a'); + link.href = globalThis.URL.createObjectURL(blob); + link.setAttribute('download', fileName); + document.body.append(link); + link.click(); + link.remove(); + globalThis.URL.revokeObjectURL(link.href); // Clean up +}; + +const createICSContent = (event: DonationEvent): string => { const endTime = new Date(new Date(event.date).getTime() + 60 * 60 * 1000); return [ - 'BEGIN:VCALENDAR', - 'VERSION:2.0', - 'PRODID:-//OpenHands//Donation Event//EN', 'BEGIN:VEVENT', `DTSTART:${formatDate(event.date)}`, `DTEND:${formatDate(endTime.toISOString())}`, @@ -21,62 +29,36 @@ const createICSContent = (event: DonationEvent) => { .join(', ')}`, `UID:${event.eventId}-${formatDate(event.date)}`, 'END:VEVENT', - 'END:VCALENDAR', ].join('\r\n'); }; -const generateICSFile = (event: DonationEvent) => { - const icsContent = createICSContent(event); - const blob = new Blob([icsContent], { - type: 'text/calendar;charset=utf-8', - }); - const link = document.createElement('a'); - link.href = globalThis.URL.createObjectURL(blob); - link.setAttribute( - 'download', - `openhands-donation-event-${event.eventId}.ics` - ); - document.body.append(link); - link.click(); - link.remove(); - globalThis.URL.revokeObjectURL(link.href); -}; - const createCombinedICSContent = (events: DonationEvent[]): string => { + const eventContents = events + .map((event) => createICSContent(event)) + .join('\r\n'); return [ 'BEGIN:VCALENDAR', 'VERSION:2.0', 'PRODID:-//OpenHands//Combined Donation Events//EN', - ...events.flatMap((event) => { - const endTime = new Date(new Date(event.date).getTime() + 60 * 60 * 1000); - return [ - 'BEGIN:VEVENT', - `DTSTART:${formatDate(event.date)}`, - `DTEND:${formatDate(endTime.toISOString())}`, - `SUMMARY:${event.title}`, - `DESCRIPTION:Items: ${event.supplies - .map((supply) => `${supply.itemName} (${supply.quantityProvided})`) - .join(', ')}`, - `UID:${event.eventId}-${formatDate(event.date)}`, - 'END:VEVENT', - ]; - }), + eventContents, + 'END:VCALENDAR', + ].join('\r\n'); +}; + +const generateICSFile = (event: DonationEvent): void => { + const icsContent = [ + 'BEGIN:VCALENDAR', + 'VERSION:2.0', + 'PRODID:-//OpenHands//Donation Event//EN', + createICSContent(event), 'END:VCALENDAR', ].join('\r\n'); + downloadICSFile(`openhands-donation-event-${event.eventId}.ics`, icsContent); }; -const generateCombinedICSFile = (events: DonationEvent[]) => { +const generateCombinedICSFile = (events: DonationEvent[]): void => { const icsContent = createCombinedICSContent(events); - const blob = new Blob([icsContent], { - type: 'text/calendar;charset=utf-8', - }); - const link = document.createElement('a'); - link.href = globalThis.URL.createObjectURL(blob); - link.setAttribute('download', 'openhands-donation-events.ics'); - document.body.append(link); - link.click(); - link.remove(); - globalThis.URL.revokeObjectURL(link.href); // Clean up + downloadICSFile('openhands-donation-events.ics', icsContent); }; export { generateICSFile, generateCombinedICSFile };