feat: accept amount of days to fetch
This commit is contained in:
1 parent
e3072755e7
commit
176602d9bb
1 file changed
+19
-6
+19
-6
@@ -1,6 +1,7 @@
|
||||
import ical, { VEvent } from 'node-ical';
|
||||
interface PageProps {
|
||||
params: Promise<{
|
||||
'days': number
|
||||
'ical-link': string[];
|
||||
}>;
|
||||
}
|
||||
@@ -19,8 +20,13 @@ function getIcalDate(dateValue: any): Date {
|
||||
}
|
||||
|
||||
export default async function EvaluationPage({ params }: PageProps) {
|
||||
// get the URL from the own URL (/evaluation/<ics-link>)
|
||||
// get all 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'];
|
||||
let rawUrl = pathSegments.join('/');
|
||||
rawUrl = decodeURIComponent(rawUrl);
|
||||
@@ -35,12 +41,19 @@ export default async function EvaluationPage({ params }: PageProps) {
|
||||
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'
|
||||
);
|
||||
|
||||
// calculate range of time
|
||||
const now = new Date();
|
||||
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 (
|
||||
<main style={{ padding: '2rem', fontFamily: 'sans-serif' }}>
|
||||
Reference in new issue
Block a user