Skip to content

Commit f0aeca4

Browse files
committed
Added calendar export
1 parent 13d9353 commit f0aeca4

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/lib/utils.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
formatMultiResult,
88
} from '@wca/helpers';
99
import { format, parseISO } from 'date-fns';
10+
import * as ics from 'ics'
1011

1112
export const byName = (a: { name: string }, b: { name: string }) => a.name.localeCompare(b.name);
1213
export const byDate = <T>(a: T & { startTime: string }, b: T & { startTime: string }) =>
@@ -148,3 +149,77 @@ export const renderResultByEventId = (
148149

149150
return formatCentiseconds(result as number);
150151
};
152+
153+
const AssignmentCodeDescription = {
154+
'staff-scrambler': 'Scrambling for:',
155+
'staff-runner': 'Runner for:',
156+
'staff-judge': 'Judging for:',
157+
'staff-dataentry': 'Data Entry for:',
158+
'staff-announcer': 'Announcing for:',
159+
'staff-delegate': 'Delegating for:',
160+
'competitor': 'Competing in:'
161+
};
162+
163+
export const generateIcs = (assignments, fileName) => {
164+
if (!assignments) {
165+
return;
166+
}
167+
168+
console.log(assignments);
169+
170+
let events: { title: string; description: string; location: string; start: ics.DateArray; end: ics.DateArray }[] = [];
171+
172+
assignments.forEach(item => {
173+
const titleFormatted = `${AssignmentCodeDescription[item.assignmentCode]} for ${item.activity.name}`;
174+
const startDate = new Date(item.activity.startTime);
175+
const endDate = new Date(item.activity.endTime);
176+
177+
const startDateArray: ics.DateArray = [
178+
startDate.getFullYear(),
179+
startDate.getMonth() + 1, // Months are 1-based in ics format
180+
startDate.getDate(),
181+
startDate.getHours(),
182+
startDate.getMinutes(),
183+
];
184+
185+
const endDateArray: ics.DateArray = [
186+
endDate.getFullYear(),
187+
endDate.getMonth() + 1,
188+
endDate.getDate(),
189+
endDate.getHours(),
190+
endDate.getMinutes(),
191+
];endDate
192+
193+
const icalEvent = {
194+
title: titleFormatted,
195+
description: item.activity.name,
196+
location: item.activity.parent.room.name,
197+
start: startDateArray,
198+
end: endDateArray,
199+
};
200+
201+
events.push(icalEvent);
202+
});
203+
204+
const { error, value } = ics.createEvents(events);
205+
206+
if (error) {
207+
console.log(error);
208+
return;
209+
}
210+
211+
if (!value) {
212+
return;
213+
}
214+
215+
const blob = new Blob([value], { type: 'text/calendar' });
216+
const a = document.createElement('a');
217+
a.href = URL.createObjectURL(blob);
218+
a.download = `fileName`;
219+
a.style.display = 'none';
220+
221+
document.body.appendChild(a);
222+
a.click();
223+
224+
document.body.removeChild(a);
225+
}

src/pages/Competition/Person/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Link, useParams } from 'react-router-dom';
77
import { useWCIF } from '../WCIFProvider';
88
import { ActivityWithRoomOrParent, parseActivityCode, rooms } from '../../../lib/activities';
99
import AssignmentLabel from '../../../components/AssignmentLabel/AssignmentLabel';
10-
import { formatDate, formatToParts, roundTime } from '../../../lib/utils';
10+
import { formatDate, formatToParts, roundTime, generateIcs } from '../../../lib/utils';
1111
import DisclaimerText from '../../../components/DisclaimerText';
1212
import { shortEventNameById } from '../../../lib/events';
1313
import classNames from 'classnames';
@@ -445,6 +445,15 @@ export default function PersonPage() {
445445
) : (
446446
<div>No Assignments</div>
447447
)}
448+
<div className="flex items-center justify-center">
449+
<button
450+
onClick={() => generateIcs(assignmentsWithParsedDate, `${wcif?.name}: ${person.name}`)}
451+
className="my-2 bg-white text-blue-500 border border-blue-500 p-2 rounded-md"
452+
style={{ width: '300px' }}
453+
>
454+
Download Calendar Export
455+
</button>
456+
</div>
448457
</div>
449458
);
450459
}

0 commit comments

Comments
 (0)