feat: fetch and read calendar
This commit is contained in:
1 parent
1b2df1912a
commit
e3072755e7
3 files changed
+100
-4
No files matched your search
@@ -1,20 +1,78 @@
|
||||
import ical, { VEvent } from 'node-ical';
|
||||
interface PageProps {
|
||||
params: Promise<{
|
||||
'ical-link': string[];
|
||||
}>;
|
||||
}
|
||||
|
||||
function getIcalText(field: any): string {
|
||||
if (!field) return '';
|
||||
if (typeof field === 'string') return field;
|
||||
if (typeof field === 'object' && 'val' in field) return field.val as string;
|
||||
return String(field);
|
||||
}
|
||||
|
||||
function getIcalDate(dateValue: any): Date {
|
||||
if (!dateValue) return new Date(0); // Fallback: 01.01.1970, falls kein Datum da ist
|
||||
if (dateValue instanceof Date) return dateValue;
|
||||
return new Date(dateValue);
|
||||
}
|
||||
|
||||
export default async function EvaluationPage({ params }: PageProps) {
|
||||
// get the URL from the own URL (/evaluation/<ics-link>)
|
||||
const resolvedParams = await params;
|
||||
const pathSegments = resolvedParams['ical-link'];
|
||||
|
||||
let rawUrl = pathSegments.join('/');
|
||||
|
||||
rawUrl = decodeURIComponent(rawUrl);
|
||||
|
||||
const icalUrl = rawUrl.replace(/^(https?):\/+/, '$1://');
|
||||
|
||||
// fetch the file
|
||||
const response = await fetch(icalUrl, { next: { revalidate: 3600 } });
|
||||
if (!response.ok) {
|
||||
return <div>Fehler beim Laden der Kalenderdaten. Status: {response.status}</div>;
|
||||
}
|
||||
|
||||
const icalData = await response.text();
|
||||
const parsedData = ical.sync.parseICS(icalData);
|
||||
|
||||
// filter data (needed: "VEVENT")
|
||||
const events = Object.values(parsedData).filter(
|
||||
(item): item is VEvent => item !== undefined && item.type === 'VEVENT'
|
||||
);
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<p>iCal-Adresse: {icalUrl}</p>
|
||||
<main style={{ padding: '2rem', fontFamily: 'sans-serif' }}>
|
||||
<h1>Meine Kalender-Events</h1>
|
||||
|
||||
{events.length === 0 ? (
|
||||
<p>Keine Termine gefunden.</p>
|
||||
) : (
|
||||
<ul style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
|
||||
{events.map((event, index) => {
|
||||
const startDate = getIcalDate(event.start);
|
||||
const endDate = getIcalDate(event.end);
|
||||
|
||||
return (
|
||||
<li key={index} style={{ border: '1px solid #ccc', padding: '1rem', borderRadius: '8px' }}>
|
||||
{/* HIER WENDEN WIR DIE HILFSFUNKTION AN */}
|
||||
<h2>{getIcalText(event.summary)}</h2>
|
||||
<p>
|
||||
<strong>Start:</strong> {startDate.toLocaleString('de-DE')} <br />
|
||||
<strong>Ende:</strong> {endDate.toLocaleString('de-DE')}
|
||||
</p>
|
||||
{event.location && (
|
||||
<p><strong>Ort:</strong> {getIcalText(event.location)}</p>
|
||||
)}
|
||||
{event.description && (
|
||||
<p><strong>Beschreibung:</strong> {getIcalText(event.description)}</p>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user