import { Calendar } from "./Icons"; const API_URL = import.meta.env.VITE_API_URL; interface Event { id: number; title: string; description?: string; date: Date; host_id: number; image_url?: string; host: { id: number; name: string; email: string; }; rsvps: { id: number; name: string; email: string; }[]; } const loadEventsData = async (): Promise => { try { const response = await fetch(`${API_URL}/events`); return response.json(); } catch (e) { console.error(e); return []; } }; export const EventModal = (event: Event) => { const formId = `rsvp-form-${event.id}`; const modalId = `modal-event-${event.id}`; return `

RSVP to ${event.title}

`; }; export const EventCard = (e: Event) => { const eventDate = new Date(e.date); const isPast = eventDate < new Date(); return `
${e.image_url && `${e.title} thumbnail`}

${e.title}

${Calendar} ${eventDate.toLocaleDateString()}

Host: ${e.host?.name || `User ${e.host_id}`}

${e.description ? `

${e.description}

` : ""}
${e.rsvps?.length || 0} ${isPast ? "went" : "going"} ${ !isPast ? ` ` : "" }
${EventModal(e)}
`; }; export const EventsSection = (title: string, events: Event[]) => { return `

${title} events

${events.map((e) => EventCard(e)).join("") || "No events"}
`; }; // IIFE to asynchronously load the Event data before exporting the component // https://developer.mozilla.org/en-US/docs/Glossary/IIFE export const Events = await (async () => { const all = await loadEventsData(); const past = all.filter((e) => new Date(e.date) < new Date()); const upcoming = all.filter((e) => new Date(e.date) > new Date()); return ` ${EventsSection("Upcoming", upcoming)} ${EventsSection("Past", past)} `; })();