1- import { EventAttributes , createEvents } from 'ics' ;
21import React , { useState } from 'react' ;
3- import { AbsenceWithRelations } from '../../app/api/getAbsences/route' ;
2+ import { createCalendarFile } from '../../app/api/ics/[id]/ics' ;
3+ import { AbsenceWithRelations } from '../../app/api/getAbsences/absences' ;
4+ import { convertAbsenceToICSEvent } from '../../app/api/ics/[id]/ics' ;
45
56export default function CalendarDownload ( ) {
67 const [ error , setError ] = useState < string | null > ( null ) ;
78
8- const searchAbsences = async ( ) : Promise < EventAttributes [ ] > => {
9- try {
10- const res = await fetch ( '/api/getAbsences/' ) ;
11- if ( ! res . ok ) {
12- throw new Error ( `Failed to fetch: ${ res . statusText } ` ) ;
13- }
14- const data = await res . json ( ) ;
15- if ( ! data . events || ! Array . isArray ( data . events ) ) {
16- throw new Error ( 'Invalid data format.' ) ;
17- }
18- return data . events . map ( ( absence : AbsenceWithRelations ) => {
19- const substituteTeacherString = absence . substituteTeacher
20- ? `(${ absence . substituteTeacher . firstName } ${ absence . substituteTeacher . lastName [ 0 ] } )`
21- : '' ;
22- const lessonString = absence . lessonPlan || 'Lesson Plan Not Submitted' ;
23- const notesLine = absence . notes ? `\nNotes: ${ absence . notes } ` : '' ;
24-
25- const startDate = new Date ( absence . lessonDate ) ;
26- const endDate = new Date ( absence . lessonDate ) ;
27- endDate . setDate ( startDate . getDate ( ) + 1 ) ;
28-
29- return {
30- start : [
31- startDate . getFullYear ( ) ,
32- startDate . getMonth ( ) + 1 ,
33- startDate . getDate ( ) ,
34- ] ,
35- end : [
36- endDate . getFullYear ( ) ,
37- endDate . getMonth ( ) + 1 ,
38- endDate . getDate ( ) ,
39- ] ,
40- title : `${ absence . subject . name } : ${ absence . absentTeacher . firstName } ${ absence . absentTeacher . lastName [ 0 ] } ${ substituteTeacherString } ` ,
41- description : `Subject: ${ absence . subject . name } \nLesson Plan: ${ lessonString } ${ notesLine } ` ,
42- location : absence . location . name ,
43- } ;
44- } ) ;
45- } catch ( err ) {
46- console . error ( 'Error fetching absences:' , err ) ;
47- throw err ;
48- }
49- } ;
50-
51- const createCalendarFile = ( events : EventAttributes [ ] ) : Promise < File > => {
52- return new Promise ( ( resolve , reject ) => {
53- createEvents ( events , ( error , value ) => {
54- if ( error ) {
55- console . error ( 'Error creating events:' , error ) ;
56- reject ( error ) ;
57- } else {
58- resolve ( new File ( [ value ] , 'Absences.ics' , { type : 'text/calendar' } ) ) ;
59- }
60- } ) ;
61- } ) ;
62- } ;
9+ const CALENDAR_NAME : string = 'Sistema Absences' ;
6310
6411 const downloadFile = ( file : File ) => {
6512 const url = URL . createObjectURL ( file ) ;
@@ -77,8 +24,21 @@ export default function CalendarDownload() {
7724 setError ( null ) ;
7825
7926 try {
80- const events = await searchAbsences ( ) ;
81- const file = await createCalendarFile ( events ) ;
27+ const res = await fetch ( '/api/getAbsences/' ) ;
28+
29+ if ( ! res . ok ) {
30+ throw new Error ( `Failed to fetch: ${ res . statusText } ` ) ;
31+ }
32+ const data = await res . json ( ) ;
33+ if ( ! data . events || ! Array . isArray ( data . events ) ) {
34+ throw new Error ( 'Invalid data format.' ) ;
35+ }
36+
37+ const icsEvents = data . events . map ( ( eventData : AbsenceWithRelations ) =>
38+ convertAbsenceToICSEvent ( eventData , CALENDAR_NAME )
39+ ) ;
40+
41+ const file = await createCalendarFile ( icsEvents ) ;
8242 downloadFile ( file ) ;
8343 } catch ( error ) {
8444 console . error ( 'Error during download process:' , error ) ;
0 commit comments