import { Calendar } from './Icons.js'; const API_URL = import.meta.env.VITE_API_URL; const loadEventsData = async () => { try { const response = await fetch(`${API_URL}/events`); return response.json(); } catch (e) { console.error(e); } } export const EventModal = (event) => { const formId = `rsvp-form-${event.ID}`; const modalId = `modal-event-${event.id}` return `

RSVP to ${event.title}

` } export const EventCard = (e) => { 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}

`}
${EventModal(e)}
` } export const EventsSection = (title, events) => { 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)} `})()