Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement calendar export #20

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default [
'unicorn/filename-case': 'off',
'unicorn/expiring-todo-comments': 'off',
'@typescript-eslint/no-unused-expressions': 'off',
'unicorn/prefer-string-replace-all': 'off',
},
},
];
12 changes: 12 additions & 0 deletions src/components/Schedule/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import {
DialogContent,
DialogTitle,
Button,
IconButton,
} from '@mui/material';
import EventIcon from '@mui/icons-material/Event';
import type { Dayjs } from 'dayjs';
import dayjs from 'dayjs';

import EventsCalendar from './EventsCalendar';

import generateICSFile from '@/utils/generateICSFile';

interface ScheduleBaseProps {
events: DonationEvent[];
title: string;
Expand Down Expand Up @@ -155,6 +159,14 @@ const ScheduleBase = ({ events, title, description }: ScheduleBaseProps) => {
{supply.itemName} - {supply.quantityProvided} provided
</Typography>
))}
<IconButton
color='primary'
onClick={() => generateICSFile(selectedEvent)}
sx={{ mt: 2 }}
>
<EventIcon />
Export to Calendar
</IconButton>
</>
)}
</DialogContent>
Expand Down
43 changes: 43 additions & 0 deletions src/utils/generateICSFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const formatDate = (dateString: string) => {
const date = new Date(dateString);
// Replace colons, dashes, and milliseconds
return date
.toISOString()
.replace(/[:-]/g, '') // Remove colons and dashes
.replace(/\.\d{3}/, ''); // Remove milliseconds
};

const createICSContent = (event: DonationEvent) => {
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())}`,
`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'); // use CRLF for better compatibility
};

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', `donation-event-${event.eventId}.ics`);
document.body.append(link);
link.click();
link.remove();
globalThis.URL.revokeObjectURL(link.href); // Clean up
};

export default generateICSFile;
Loading