Use state instead of getElementById

This commit is contained in:
regalijan 2023-10-20 12:45:54 -04:00
parent ea1f54fd04
commit c609ab4e0e
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520

View File

@ -22,19 +22,16 @@ import { useState } from "react";
export default function (props: GameAppealProps) { export default function (props: GameAppealProps) {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [percentage, setPercentage] = useState(0);
async function performAction(action: "accept" | "deny"): Promise<void> { async function performAction(action: "accept" | "deny"): Promise<void> {
setLoading(true); setLoading(true);
const statsReduction = parseInt(
(document.getElementById("reductPercentage") as HTMLInputElement | null)
?.value ?? "0",
);
const actionResponse = await fetch( const actionResponse = await fetch(
`/api/game-appeals/${props.id}/${action}`, `/api/game-appeals/${props.id}/${action}`,
{ {
body: JSON.stringify({ body: JSON.stringify({
statsReduction: isNaN(statsReduction) ? 0 : statsReduction, statsReduction: isNaN(percentage) ? 0 : percentage,
}), }),
headers: { headers: {
"content-type": "application/json", "content-type": "application/json",
@ -112,13 +109,13 @@ export default function (props: GameAppealProps) {
</ModalHeader> </ModalHeader>
<ModalBody> <ModalBody>
<Input <Input
id="reductPercentage"
onBeforeInput={(e) => { onBeforeInput={(e) => {
const value = (e.target as EventTarget & { value: string }) const value = (e.target as EventTarget & { value: string })
.value; .value;
return !value.match(/\D/); return !value.match(/\D/);
}} }}
onChange={(e) => setPercentage(parseInt(e.target.value))}
placeholder="Number between 0 and 100" placeholder="Number between 0 and 100"
/> />
</ModalBody> </ModalBody>