Skip to content

Commit

Permalink
Merge branch 'main' into filter
Browse files Browse the repository at this point in the history
  • Loading branch information
WelldoneM authored Dec 6, 2024
2 parents 1aefcf1 + d5c1b56 commit 06dad25
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 55 deletions.
24 changes: 17 additions & 7 deletions src/components/Schedule/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,35 @@ const ScheduleBase = ({ events, title, description }: ScheduleBaseProps) => {
setSelectedDate={setSelectedDate}
/>

<Box mt={2}>
<>
<Box
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: '1rem',
flexDirection: 'column',
gap: '0.5rem',
}}
>
<Typography variant='h6'>
Events for {selectedDate?.format('MMMM D, YYYY')}:
</Typography>
<Button
onClick={() => generateCombinedICSFile(events)}
variant='contained'
sx={{
p: '0.5rem 1.5rem',
display: 'flex',
alignItems: 'center',
}}
>
<FileDownload sx={{ marginRight: '0.25rem' }} />
Export All Events to Calendar
<FileDownload sx={{ marginRight: '0.5rem' }} />
Export All to Calendar
</Button>
<Typography
variant='h6'
textAlign='center'
>
Events for {selectedDate?.format('MMMM D, YYYY')}:
</Typography>
</Box>

{eventsForSelectedDate.length > 0 ? (
Expand Down Expand Up @@ -154,7 +164,7 @@ const ScheduleBase = ({ events, title, description }: ScheduleBaseProps) => {
No events for this date.
</Typography>
)}
</Box>
</>

{/* Event Details Modal */}
<Dialog
Expand Down
78 changes: 30 additions & 48 deletions src/utils/generateICSFile.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const formatDate = (dateString: string) => {
const formatDate = (dateString: string): string => {
const date = new Date(dateString);
return date
.toISOString()
.replace(/[:-]/g, '')
.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())}`,
Expand All @@ -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 };

0 comments on commit 06dad25

Please sign in to comment.