From 542f77459b98935cc4bc899a185473cc9646f0de Mon Sep 17 00:00:00 2001 From: minhd-vu Date: Sun, 25 Feb 2024 23:41:26 -0500 Subject: [PATCH] add confirm and deny kill --- components/ConfirmKill.tsx | 29 +++++++++++++++++++++++++++++ components/DenyKill.tsx | 29 +++++++++++++++++++++++++++++ components/KillTarget.tsx | 23 +++++++++++++++++------ components/Party.tsx | 18 ++++++++++++++++-- 4 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 components/ConfirmKill.tsx create mode 100644 components/DenyKill.tsx diff --git a/components/ConfirmKill.tsx b/components/ConfirmKill.tsx new file mode 100644 index 0000000..edf99ed --- /dev/null +++ b/components/ConfirmKill.tsx @@ -0,0 +1,29 @@ +"use client"; + +import { useContext } from "react"; +import { useSWRConfig } from "swr"; +import { ErrorContext } from "./App"; + +export default function ConfirmKill() { + const { mutate } = useSWRConfig(); + const { setError } = useContext(ErrorContext); + + async function onClick() { + const res = await fetch("/api/kill/confirm", { + method: "POST", + }); + + if (!res.ok) { + setError(await res.json()); + return; + } + + mutate("/api/user"); + } + + return ( + + ); +} diff --git a/components/DenyKill.tsx b/components/DenyKill.tsx new file mode 100644 index 0000000..48b7ba8 --- /dev/null +++ b/components/DenyKill.tsx @@ -0,0 +1,29 @@ +"use client"; + +import { useContext } from "react"; +import { useSWRConfig } from "swr"; +import { ErrorContext } from "./App"; + +export default function DenyKill() { + const { mutate } = useSWRConfig(); + const { setError } = useContext(ErrorContext); + + async function onClick() { + const res = await fetch("/api/kill/deny", { + method: "POST", + }); + + if (!res.ok) { + setError(await res.json()); + return; + } + + mutate("/api/user"); + } + + return ( + + ); +} diff --git a/components/KillTarget.tsx b/components/KillTarget.tsx index 2a2c516..8378274 100644 --- a/components/KillTarget.tsx +++ b/components/KillTarget.tsx @@ -1,26 +1,37 @@ "use client"; +import { useContext } from "react"; import { useSWRConfig } from "swr"; +import { ErrorContext } from "./App"; -export default function KillTarget() { +export default function KillTarget({ pending }: { pending: boolean }) { const { mutate } = useSWRConfig(); + const { setError } = useContext(ErrorContext); - async function killTarget() { + async function onClick() { const res = await fetch("/api/kill", { method: "POST", }); if (!res.ok) { - throw new Error(await res.json()); + setError(await res.json()); + return; } - console.log(await res.json()); - mutate("/api/user"); } + if (pending) { + return ( + + ); + } + return ( - ); diff --git a/components/Party.tsx b/components/Party.tsx index a8fe4e4..5dbadab 100644 --- a/components/Party.tsx +++ b/components/Party.tsx @@ -16,8 +16,14 @@ import KillTarget from "./KillTarget"; import PartyCard from "./PartyCard"; import KickPlayer from "./KickPlayer"; import { User } from "@/lib/user"; +import ConfirmKill from "./ConfirmKill"; +import DenyKill from "./DenyKill"; +import { useContext } from "react"; +import { ErrorContext } from "./App"; export default function Party() { + const { setError } = useContext(ErrorContext); + const fetcher: Fetcher = (url) => fetch(url).then((res) => res.json()); @@ -50,14 +56,22 @@ export default function Party() { if (party.started) { if (!user.target) { - throw new Error("User does not have a target"); + setError("User has no target"); + return; } return (

Target: {user.target.name}

- + {user.pending ? ( + <> + + + + ) : ( + + )} {party.adminId === user.id && }