Feat/evaluation #1
+19
-6
@@ -1,6 +1,7 @@
|
|||||||
import ical, { VEvent } from 'node-ical';
|
import ical, { VEvent } from 'node-ical';
|
||||||
interface PageProps {
|
interface PageProps {
|
||||||
params: Promise<{
|
params: Promise<{
|
||||||
|
'days': number
|
||||||
'ical-link': string[];
|
'ical-link': string[];
|
||||||
}>;
|
}>;
|
||||||
}
|
}
|
||||||
@@ -19,8 +20,13 @@ function getIcalDate(dateValue: any): Date {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default async function EvaluationPage({ params }: PageProps) {
|
export default async function EvaluationPage({ params }: PageProps) {
|
||||||
// get the URL from the own URL (/evaluation/<ics-link>)
|
// get all params
|
||||||
const resolvedParams = await params;
|
const resolvedParams = await params;
|
||||||
|
|
||||||
|
// get amount of days to fetch
|
||||||
|
const daysToFetch = Number(resolvedParams['days']) || 7; //fallback to 7 days
|
||||||
|
|
||||||
|
// get the URL from the own URL (/evaluation/<ics-link>)
|
||||||
const pathSegments = resolvedParams['ical-link'];
|
const pathSegments = resolvedParams['ical-link'];
|
||||||
let rawUrl = pathSegments.join('/');
|
let rawUrl = pathSegments.join('/');
|
||||||
rawUrl = decodeURIComponent(rawUrl);
|
rawUrl = decodeURIComponent(rawUrl);
|
||||||
@@ -35,12 +41,19 @@ export default async function EvaluationPage({ params }: PageProps) {
|
|||||||
const icalData = await response.text();
|
const icalData = await response.text();
|
||||||
const parsedData = ical.sync.parseICS(icalData);
|
const parsedData = ical.sync.parseICS(icalData);
|
||||||
|
|
||||||
// filter data (needed: "VEVENT")
|
// calculate range of time
|
||||||
const events = Object.values(parsedData).filter(
|
const now = new Date();
|
||||||
(item): item is VEvent => item !== undefined && item.type === 'VEVENT'
|
const cutoffDate = new Date();
|
||||||
);
|
cutoffDate.setDate(now.getDate() - daysToFetch);
|
||||||
|
|
||||||
|
|
||||||
|
// filter and sort data (needed: "VEVENT"; time range)
|
||||||
|
const events = Object.values(parsedData)
|
||||||
|
.filter((item): item is VEvent => item !== undefined && item.type === 'VEVENT')
|
||||||
|
.filter((event) => {
|
||||||
|
const startDate = getIcalDate(event.start);
|
||||||
|
return startDate >= cutoffDate && startDate <= now;
|
||||||
|
})
|
||||||
|
.sort((a, b) => getIcalDate(b.start).getTime() - getIcalDate(a.start).getTime()); // newest first
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main style={{ padding: '2rem', fontFamily: 'sans-serif' }}>
|
<main style={{ padding: '2rem', fontFamily: 'sans-serif' }}>
|
||||||
Reference in new issue
Block a user