From 4e4cae7f001c3e8b44f451184a419fe0926e55fd Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:49:31 -0400 Subject: [PATCH] Create takeAction function --- components/AppealCard.tsx | 78 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/components/AppealCard.tsx b/components/AppealCard.tsx index fefe64d..2914ca2 100644 --- a/components/AppealCard.tsx +++ b/components/AppealCard.tsx @@ -6,9 +6,18 @@ import { CardFooter, CardHeader, Heading, + Modal, + ModalBody, + ModalContent, + ModalFooter, + ModalHeader, + ModalOverlay, Stack, StackDivider, Text, + Textarea, + useDisclosure, + useToast, } from "@chakra-ui/react"; import { useEffect, useState } from "react"; @@ -16,13 +25,70 @@ export default function (props: AppealCardProps) { const [dateString, setDateString] = useState( new Date(props.createdAt).toUTCString() ); + const [action, setAction] = useState(""); + const [feedback, setFeedback] = useState(""); useEffect(() => { setDateString(new Date(props.createdAt).toLocaleString()); }, [props.createdAt]); + const { isOpen, onClose, onOpen } = useDisclosure(); + + function showModal(action: "Accept" | "Deny") { + setAction(action); + onOpen(); + } + + async function takeAction(action: string) { + const actionReq = await fetch(`/api/appeals/${props.id}/${action}`, { + body: feedback ? JSON.stringify({ feedback }) : "{}", + headers: { + "content-type": "application/json", + }, + method: "POST", + }); + + if (actionReq.ok) { + useToast()({ + description: `Appeal ${action === "accept" ? "accepted" : "denied"}`, + duration: 5000, + status: "success", + title: "Success", + }); + + document.getElementById(`appeal_${props.id}`)?.remove(); + } + + useToast()({ + description: ((await actionReq.json()) as { error: string }).error, + duration: 10000, + status: "error", + title: "Oops!", + }); + } + return ( - + + + + + {action} this appeal? + + Optionally provide feedback +