Skip to content

Commit

Permalink
Testing commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ludavidca committed Oct 3, 2024
1 parent 7bb566f commit e88d190
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/pages/ics.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import React, { useState } from 'react';
import { EventAttributes, createEvents } from 'ics';

export default function CalendarDownload() {
const searchAbsences = async () => {
const [error, setError] = useState<string | null>(null);

const searchAbsences = async (): Promise<EventAttributes[]> => {
try {
const res = await fetch('/api/getAbsences/');
const data = await res.json();
const events = data.body.events;
console.log(events);
return events;
console.log('Fetched events:', events);

// Ensure events are in the correct format for ics
return events.map((event: any) => ({
start: [event.startYear, event.startMonth, event.startDay],
end: [event.endYear, event.endMonth, event.endDay],
title: event.title,
description: event.description,
// Add any other required fields
}));
} catch (err) {
console.error(err);
console.error('Error fetching absences:', err);
throw err;
}
};
Expand All @@ -18,9 +29,10 @@ export default function CalendarDownload() {
return new Promise((resolve, reject) => {
createEvents(events, (error, value) => {
if (error) {
console.error('Error creating events:', error);
reject(error);
} else {
resolve(new File([value], 'Sistema.ics', { type: 'text/calendar' }));
resolve(new File([value], 'Absences.ics', { type: 'text/calendar' }));
}
});
});
Expand All @@ -38,20 +50,26 @@ export default function CalendarDownload() {
};

const handleDownload = async (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
setError(null);

try {
event.preventDefault();
const events = await searchAbsences();
if (events.length === 0) {
throw new Error('No events found');
}
const file = await createCalendarFile(events);
downloadFile(file);
} catch (error) {
console.error('Error during download process:', error);
// Handle the error appropriately, e.g., show an error message to the user
setError('Failed to download calendar. Please try again later.');
}
};

return (
<div>
<button onClick={handleDownload}>Download iCal File</button>
{error && <p style={{ color: 'red' }}>{error}</p>}
</div>
);
}

0 comments on commit e88d190

Please sign in to comment.