diff --git a/app/.server/events.ts b/app/.server/events.ts new file mode 100644 index 0000000..a1b908f --- /dev/null +++ b/app/.server/events.ts @@ -0,0 +1,65 @@ +import { z } from "zod"; +import { marked } from "marked"; + +const EventSchema = z.object({ + id: z.number(), + name: z.string(), + slug: z.string(), + date: z.string(), + content: z.string().transform((content) => marked(content)), + results: z.boolean() + }); + +export type Event = z.infer; + + +export async function getLatestEventsAndResults() { + const [events, results] = await Promise.all([ + getEvents({ results: false, limit: 5 }).then((res) => { + return res.filter((event) => { + return new Date(event.date) >= + new Date(); + }); + }), + getEvents({ results: true, limit: 5 }), + ]); + return { + events, + results + } +} + +export function getEvents({ + results, + limit, +}: { + limit?: number; + results: boolean; +}) { + return fetch( + `${process.env.STRAPI_URL}/api/veranstaltungs?sort[0]=date:desc&populate=*&filters[results][$eq]=${results}&pagination[limit]=${limit}` + ).then((res) => res.json()).then((res) => { + const events = res.data.map((data: unknown) => { + const event = EventSchema.safeParse(data) + if(event.success) { + return event.data; + } + return null; + }).filter((event: Event | null) => event !== null) as Event[]; + return events; + }); +} + +export function getEvent({ slug }: { slug: string }) { + return fetch(` + ${process.env.STRAPI_URL}/api/veranstaltungs?sort[0]=date:desc&populate=*&filters[slug][$eq]=${slug}` + ).then((res) => res.json()).then((res) => { + const event = z.array(EventSchema).min(1).max(1).safeParse(res.data); + if(event.success) { + return event.data[0]; + } + throw new Response("Veranstaltung existiert nicht", { + status: 404, + }); + }); +} diff --git a/app/components/pages/home/events/Events.tsx b/app/components/pages/home/events/Events.tsx index a3dc05c..c307548 100644 --- a/app/components/pages/home/events/Events.tsx +++ b/app/components/pages/home/events/Events.tsx @@ -37,6 +37,13 @@ export default function Events({ results, events }: EventsProps) { ))} + {events.length === 0 && ( + +
+ Keine bevorstehenden Veranstaltungen +
+
+ )} @@ -64,6 +71,13 @@ export default function Events({ results, events }: EventsProps) { ))} + {events.length === 0 && ( + +
+ Keine Ergebnisse gefunden +
+
+ )} diff --git a/app/components/shared/list/List.tsx b/app/components/shared/list/List.tsx index 6c06c17..6968bd4 100644 --- a/app/components/shared/list/List.tsx +++ b/app/components/shared/list/List.tsx @@ -1,6 +1,6 @@ export default function List({ children }: { children: React.ReactNode }) { return ( -