Create submit function
This commit is contained in:
parent
c97029e510
commit
387f78be7d
@ -10,6 +10,7 @@ import {
|
|||||||
ModalOverlay,
|
ModalOverlay,
|
||||||
Select,
|
Select,
|
||||||
Text,
|
Text,
|
||||||
|
useToast,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
|
|
||||||
export default function (props: { isOpen: boolean; onClose: () => void }) {
|
export default function (props: { isOpen: boolean; onClose: () => void }) {
|
||||||
@ -22,7 +23,51 @@ export default function (props: { isOpen: boolean; onClose: () => void }) {
|
|||||||
|
|
||||||
props.onClose();
|
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 (
|
return (
|
||||||
<Modal isCentered isOpen={props.isOpen} onClose={props.onClose}>
|
<Modal isCentered isOpen={props.isOpen} onClose={props.onClose}>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user