From 1dc4f56425209d4ce1d7bb78ec8b5e7b5a755a82 Mon Sep 17 00:00:00 2001 From: Anjana Vakil Date: Tue, 26 Aug 2025 12:40:16 -0500 Subject: reset --- frontend/src/components/Events.js | 106 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 frontend/src/components/Events.js (limited to 'frontend/src/components/Events.js') diff --git a/frontend/src/components/Events.js b/frontend/src/components/Events.js new file mode 100644 index 0000000..bca948a --- /dev/null +++ b/frontend/src/components/Events.js @@ -0,0 +1,106 @@ +import { Calendar } from './Icons.js'; + +const API_URL = '/api'; + +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}

`} +
+
+ + ${e.rsvps.length} + ${isPast ? 'went' : 'going'} + + ${!isPast ? ` + `: ''} +
+ ${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)} +`})() \ No newline at end of file -- cgit v1.3