import { Box, Button, Card, CardBody, CardFooter, CardHeader, Heading, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, ModalOverlay, Stack, StackDivider, Text, Textarea, useDisclosure, useToast } from "@chakra-ui/react"; import { useEffect, useState } from "react"; export default function(props: AppealCardProps & { port?: MessagePort }) { const [dateString, setDateString] = useState( new Date(props.created_at).toUTCString() ); const [action, setAction] = useState(""); const [feedback, setFeedback] = useState(""); const [loading, setLoading] = useState(false); const toast = useToast(); useEffect(() => { setDateString(new Date(props.created_at).toLocaleString()); }, [props.created_at]); const { isOpen, onClose, onOpen } = useDisclosure(); function showModal(action: "Accept" | "Deny") { setAction(action); onOpen(); } async function takeAction(action: string) { setLoading(true); const actionReq = await fetch(`/api/appeals/${props.id}/${action}`, { body: feedback ? JSON.stringify({ feedback }) : "{}", headers: { "content-type": "application/json" }, method: "POST" }); if (actionReq.ok) { setLoading(false); toast({ description: `Appeal ${action === "accept" ? "accepted" : "denied"}`, duration: 5000, status: "success", title: "Success" }); document.getElementById(`appeal_${props.id}`)?.remove(); } else { let error; try { error = ((await actionReq.json()) as { error: string }).error; } catch { error = "Unknown error"; } setLoading(false); toast({ description: error, duration: 10000, status: "error", title: "Oops!" }); } onClose(); setLoading(false); props.port?.postMessage(`appeal_${props.id}`); } return ( {action} this appeal? Optionally provide feedback