From e00d054b5702d70616e5d8255e0e16cf2a0228b9 Mon Sep 17 00:00:00 2001 From: Regalijan Date: Thu, 28 Mar 2024 04:37:54 -0400 Subject: [PATCH] Add historical events page --- app/routes/events-team_.historical.tsx | 190 +++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 app/routes/events-team_.historical.tsx diff --git a/app/routes/events-team_.historical.tsx b/app/routes/events-team_.historical.tsx new file mode 100644 index 0000000..056be7d --- /dev/null +++ b/app/routes/events-team_.historical.tsx @@ -0,0 +1,190 @@ +import { + Box, + Button, + Card, + CardBody, + CardFooter, + Container, + Heading, + HStack, + NumberDecrementStepper, + NumberIncrementStepper, + NumberInput, + NumberInputField, + NumberInputStepper, + Select, + Stack, + StackDivider, + Text, + useToast, + VStack, +} from "@chakra-ui/react"; +import { type LinksFunction } from "@remix-run/cloudflare"; +import { type ReactNode, useEffect, useState } from "react"; +import stylesheet from "../styles/events-team.css"; +import { useLoaderData } from "@remix-run/react"; + +export const links: LinksFunction = () => { + return [{ href: stylesheet, rel: "stylesheet" }]; +}; + +export async function loader({ context }: { context: RequestContext }) { + if (!context.data.current_user) + throw new Response(null, { + status: 401, + }); + + if ( + ![1 << 3, 1 << 4, 1 << 12].find( + (p) => context.data.current_user.permissions & p, + ) + ) + throw new Response(null, { + status: 403, + }); + + return ( + await context.env.D1.prepare("SELECT id, name FROM et_members;").all() + ).results; +} + +export default function () { + const memberData = useLoaderData(); + const [month, setMonth] = useState(new Date().getUTCMonth() + 1); + const [year, setYear] = useState(new Date().getUTCFullYear()); + const [eventCards, setEventCards] = useState([] as ReactNode[]); + const toast = useToast(); + + async function getEvents() { + const eventsResp = await fetch( + `/api/events-team/events/list?month=${month}&year=${year}`, + ).catch(() => {}); + + if (!eventsResp?.ok) { + let errorMsg = "Unknown error"; + + try { + errorMsg = ((await eventsResp?.json()) as { error: string }).error; + } catch {} + + toast({ + description: errorMsg, + status: "error", + title: "Failed to load events", + }); + + return; + } + + const eventsData: { [k: string]: any }[] = await eventsResp.json(); + const newEventCards = []; + + for (const event of eventsData) { + let memberName = event.created_by; + const memberDataIdx = memberData.findIndex( + (m) => m.id === event.created_by, + ); + + if (memberDataIdx !== -1) + memberName = `${memberData[memberDataIdx].name} (${event.created_by})`; + + newEventCards.push( + + + } spacing="4"> + + Date + + {event.year}-${event.month}-${event.day} + + + + Event Type + + {event.type.toUpperCase()} + + + + Event Details + + {event.details} + + + {event.type === "rotw" ? ( + + Riddle Answer + + {event.answer} + + + ) : null} + + Host + + {memberName} + + + + + + + Status:{" "} + {event.pending + ? "Not completed" + : event.approved + ? "Approved" + : "Denied"} + + + , + ); + } + + setEventCards(newEventCards); + } + + useEffect(() => { + (async () => { + await getEvents(); + })(); + }, []); + + return ( + + + + + + + + + + + + + {eventCards} + + ); +}