From 387f78be7d0908dcbecfba67de8196ef2f316ec4 Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:49:35 -0400 Subject: [PATCH] Create submit function --- components/NewInfractionModal.tsx | 47 ++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/components/NewInfractionModal.tsx b/components/NewInfractionModal.tsx index 67859c4..e3fea79 100644 --- a/components/NewInfractionModal.tsx +++ b/components/NewInfractionModal.tsx @@ -10,6 +10,7 @@ import { ModalOverlay, Select, Text, + useToast, } from "@chakra-ui/react"; export default function (props: { isOpen: boolean; onClose: () => void }) { @@ -22,7 +23,51 @@ export default function (props: { isOpen: boolean; onClose: () => void }) { props.onClose(); } - async function submit() {} + async function submit() { + const form = new FormData(); + const { files } = document.getElementById("evidence") as HTMLInputElement; + const punishment = ( + document.getElementById("punishment") as unknown as HTMLSelectElement + ).item(0)?.value as string; + const { value: user } = document.getElementById("user") as HTMLInputElement; + + form.append("user", user); + form.append("punishment", punishment); + + if (files) { + for (let i = 0; i < files.length; i++) + form.append(`file${i}`, files[i], files[i].name); + } + + const postReq = await fetch("/api/infractions/new", { + body: form, + method: "POST", + }); + + if (postReq.ok) { + useToast()({ + description: "Infraction created", + duration: 5000, + isClosable: true, + status: "success", + title: "Success", + }); + + props.onClose(); + + return; + } + + useToast()({ + description: `Failed to create infraction (${ + ((await postReq.json()) as { error: string }).error + })`, + duration: 5000, + isClosable: true, + status: "error", + title: "Error", + }); + } return (