Skip to content

Commit

Permalink
Revert "feat: add exporting all calendar events (#29)"
Browse files Browse the repository at this point in the history
This reverts commit 29e9f56.
  • Loading branch information
ZL-Asica authored Dec 6, 2024
1 parent 29e9f56 commit afdcb5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 72 deletions.
36 changes: 9 additions & 27 deletions src/components/Schedule/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ import {
Button,
IconButton,
} from '@mui/material';
import { FileDownload, CalendarMonth } from '@mui/icons-material';
import CalendarMonthIcon from '@mui/icons-material/CalendarMonth';
import type { Dayjs } from 'dayjs';
import dayjs from 'dayjs';

import EventsCalendar from './EventsCalendar';

import {
generateICSFile,
generateCombinedICSFile,
} from '@/utils/generateICSFile';
import generateICSFile from '@/utils/generateICSFile';

interface ScheduleBaseProps {
events: DonationEvent[];
Expand Down Expand Up @@ -52,7 +49,6 @@ const ScheduleBase = ({ events, title, description }: ScheduleBaseProps) => {
<Typography
variant='h4'
mb={2}
mt={4}
textAlign='center'
>
{title}
Expand All @@ -72,26 +68,12 @@ const ScheduleBase = ({ events, title, description }: ScheduleBaseProps) => {
/>

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

Events for {selectedDate?.format('MMMM D, YYYY')}:
</Typography>
{eventsForSelectedDate.length > 0 ? (
eventsForSelectedDate.map((event_) => (
<Box
Expand Down Expand Up @@ -141,8 +123,8 @@ const ScheduleBase = ({ events, title, description }: ScheduleBaseProps) => {
onClick={() => generateICSFile(event_)}
sx={{ fontSize: 12, display: 'flex', flexDirection: 'column' }}
>
<CalendarMonth fontSize='large' />
Export to Calendar
<CalendarMonthIcon fontSize='large' />
Add to Calendar
</IconButton>
</Box>
))
Expand Down
51 changes: 6 additions & 45 deletions src/utils/generateICSFile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const formatDate = (dateString: string) => {
const date = new Date(dateString);
// Replace colons, dashes, and milliseconds
return date
.toISOString()
.replace(/[:-]/g, '')
.replace(/\.\d{3}/, '');
.replace(/[:-]/g, '') // Remove colons and dashes
.replace(/\.\d{3}/, ''); // Remove milliseconds
};

const createICSContent = (event: DonationEvent) => {
Expand All @@ -22,7 +23,7 @@ const createICSContent = (event: DonationEvent) => {
`UID:${event.eventId}-${formatDate(event.date)}`,
'END:VEVENT',
'END:VCALENDAR',
].join('\r\n');
].join('\r\n'); // use CRLF for better compatibility
};

const generateICSFile = (event: DonationEvent) => {
Expand All @@ -32,51 +33,11 @@ const generateICSFile = (event: DonationEvent) => {
});
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 => {
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',
];
}),
'END:VCALENDAR',
].join('\r\n');
};

const generateCombinedICSFile = (events: DonationEvent[]) => {
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');
link.setAttribute('download', `donation-event-${event.eventId}.ics`);
document.body.append(link);
link.click();
link.remove();
globalThis.URL.revokeObjectURL(link.href); // Clean up
};

export { generateICSFile, generateCombinedICSFile };
export default generateICSFile;

0 comments on commit afdcb5d

Please sign in to comment.