77 formatMultiResult ,
88} from '@wca/helpers' ;
99import { format , parseISO } from 'date-fns' ;
10+ import * as ics from 'ics'
1011
1112export const byName = ( a : { name : string } , b : { name : string } ) => a . name . localeCompare ( b . name ) ;
1213export 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+ }
0 commit comments