import { Box, Button, Card, CardBody, CardFooter, Container, Flex, Heading, Link, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, Stack, StackDivider, Text, useDisclosure, useToast, VStack, } from "@chakra-ui/react"; import { useLoaderData } from "@remix-run/react"; import { type ReactNode, useState } from "react"; 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, }); const now = new Date(); const monthEventList = await context.env.D1.prepare( "SELECT answer, approved, created_by, day, details, id, month, pending, type, year FROM events WHERE month = ? AND year = ?;", ) .bind(now.getUTCMonth() + 1, now.getUTCFullYear()) .all(); if (monthEventList.error) throw new Response(null, { status: 500, }); return { can_approve: Boolean( [1 << 4, 1 << 12].find((p) => context.data.current_user.permissions & p), ), events: monthEventList.results, }; } export default function () { const { can_approve, events, }: { can_approve: boolean; events: { [k: string]: any }[]; } = useLoaderData(); const eventCards: ReactNode[] = []; const { isOpen, onClose, onOpen } = useDisclosure(); const toast = useToast(); const [selectedEvent, setSelectedEvent] = useState(""); async function decide(approved: boolean, eventId: string) { const decisionResp = await fetch( `/api/events-team/events/${eventId}/decision`, { body: JSON.stringify({ approved }), headers: { "content-type": "application/json", }, method: "POST", }, ); if (!decisionResp.ok) { let errorMsg = "Unknown error"; try { errorMsg = ((await decisionResp.json()) as { error: string }).error; } catch {} toast({ description: errorMsg, status: "error", title: "Oops!", }); return; } toast({ description: `Event ${approved ? "approved" : "rejected"}`, status: "success", title: "Success", }); } async function certify(eventId: string) { const certifyResp = await fetch( `/api/events-team/events/${eventId}/certify`, { body: "{}", headers: { "content-type": "application/json", }, method: "POST", }, ); if (!certifyResp.ok) { let errorMsg = "Unknown error"; try { errorMsg = ((await certifyResp.json()) as { error: string }).error; } catch {} toast({ description: errorMsg, status: "error", title: "Failed to certify game night", }); return; } toast({ description: "Game night certified", title: "Success", }); } for (const event of events) { eventCards.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 {event.created_by} {can_approve && event.pending ? ( <> ) : null} {can_approve && event.approved && event.type === "gamenight" && !event.reached_minimum_player_count ? ( <> ) : null} Status:{" "} {event.pending ? "Pending" : event.approved ? "Approved" : "Denied"} , ); } return ( Certify Game Night By certifying this game night, you confirm that the minimum number of players was met and you were provided proof. {eventCards} Book an Event
Events Team Member Management
); }