Skip to content

Commit 01ec715

Browse files
feat: Add API endpoint for upcoming events
Introduces a new API endpoint at `/api/events/upcoming`. This endpoint returns a JSON array of event calendar entries that are scheduled to start on the current day or in the future. The events are sorted by their start date in ascending order. The implementation fetches all event data, converts date strings to Date objects for accurate comparison, filters out past events, and then sorts the remaining upcoming events before returning them.
1 parent 9a993f3 commit 01ec715

File tree

4 files changed

+207
-3
lines changed

4 files changed

+207
-3
lines changed

app/(web)/page.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Hero } from "#/components/hero/hero";
22
import { AnnouncementsBlock } from "#/components/announcements/AnnouncementsBlock";
33
import { ArticlePreviewList } from "#/components/articles/ArticlePreviewList";
4+
import { EventCalendar } from "#/components/calendar/Calendar";
45
import { BlockTitle } from "#/components/block-title/BlockTitle";
56
import { JSX } from "react";
6-
import { EventList } from "#/components/events/EventList";
77

88
export default async function IndexPage(): Promise<JSX.Element | null> {
99
return (
@@ -13,7 +13,9 @@ export default async function IndexPage(): Promise<JSX.Element | null> {
1313
<div className="p-4 bg-neutral-200 flex flex-col gap-4">
1414
<div className="container flex flex-col gap-2">
1515
<BlockTitle title="Veranstaltungen und Termine" />
16-
<EventList />
16+
<div className="bg-white p-2 shadow-xl">
17+
<EventCalendar agendaOnly={true} />
18+
</div>
1719
</div>
1820
<ArticlePreviewList pageSize={10} />
1921
</div>

app/(web)/verein/veranstaltungen/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export default function Veranstaltungen() {
1414
return (
1515
<PageBase>
1616
<BlockTitle title={veranstaltungen.name} />
17-
<EventList />
17+
<div className="bg-white shadow-2xl p-2">
18+
<EventList />
19+
</div>
1820
</PageBase>
1921
);
2022
}

app/api/events/upcoming/route.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { NextResponse } from 'next/server';
2+
import path from 'path';
3+
import { getEventMatters, EventMatter, Event } from '#/content/events'; // Assuming Event is exported from #/content/events
4+
5+
export async function GET() {
6+
const eventDirectory = path.resolve('./public', 'content', 'event');
7+
const allEventMatters: EventMatter[] = getEventMatters(eventDirectory);
8+
9+
const todayAtMidnight = new Date();
10+
todayAtMidnight.setHours(0, 0, 0, 0);
11+
12+
const upcomingEvents: Event[] = [];
13+
14+
allEventMatters.forEach((eventMatter) => {
15+
eventMatter.calendarEntries.forEach((entry) => {
16+
// The 'start' and 'end' fields from gray-matter are initially strings.
17+
// They need to be converted to Date objects for comparison and for the response.
18+
const eventStartDate = new Date(entry.start);
19+
const eventEndDate = new Date(entry.end); // Also convert end date
20+
21+
if (eventStartDate >= todayAtMidnight) {
22+
// Add the event with Date objects for start/end.
23+
// The Event type defines start/end as Date.
24+
// JSON serialization will convert these Date objects to ISO strings.
25+
upcomingEvents.push({
26+
...entry,
27+
start: eventStartDate,
28+
end: eventEndDate,
29+
});
30+
}
31+
});
32+
});
33+
34+
// Sort upcoming events by their start date (earliest first)
35+
upcomingEvents.sort((a, b) => a.start.getTime() - b.start.getTime());
36+
37+
return NextResponse.json(upcomingEvents);
38+
}

0 commit comments

Comments
 (0)