From 6d74c170da5054a777640ff5944c078e70bbd5a6 Mon Sep 17 00:00:00 2001 From: minhd-vu Date: Tue, 27 Feb 2024 01:34:14 -0500 Subject: [PATCH] use onClick --- components/CreateParty.tsx | 4 ++-- components/LeaveParty.tsx | 4 ++-- components/StartGame.tsx | 4 ++-- components/StopGame.tsx | 10 +++++++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/components/CreateParty.tsx b/components/CreateParty.tsx index 62e70da..bf6953c 100644 --- a/components/CreateParty.tsx +++ b/components/CreateParty.tsx @@ -5,7 +5,7 @@ import { useSWRConfig } from "swr"; export default function CreateParty() { const { mutate } = useSWRConfig(); - async function createParty() { + async function onClick() { const res = await fetch("/api/party", { method: "POST", }); @@ -18,7 +18,7 @@ export default function CreateParty() { } return ( - ); diff --git a/components/LeaveParty.tsx b/components/LeaveParty.tsx index db88b22..e7aa626 100644 --- a/components/LeaveParty.tsx +++ b/components/LeaveParty.tsx @@ -7,7 +7,7 @@ export default function LeaveParty() { const [isLoading, setLoading] = useState(false); const { mutate } = useSWRConfig(); - async function leaveParty() { + async function onClick() { setLoading(true); const res = await fetch("/api/party/leave", { @@ -22,7 +22,7 @@ export default function LeaveParty() { } return ( - diff --git a/components/StartGame.tsx b/components/StartGame.tsx index 9b5b2d6..d79809e 100644 --- a/components/StartGame.tsx +++ b/components/StartGame.tsx @@ -8,7 +8,7 @@ export default function StartGame() { const { setError } = useContext(ErrorContext); const { mutate } = useSWRConfig(); - async function startGame() { + async function onClick() { const res = await fetch("/api/party/start", { method: "POST", }); @@ -22,7 +22,7 @@ export default function StartGame() { } return ( - ); diff --git a/components/StopGame.tsx b/components/StopGame.tsx index 06c53cf..76b51fa 100644 --- a/components/StopGame.tsx +++ b/components/StopGame.tsx @@ -1,24 +1,28 @@ "use client"; +import { useContext } from "react"; import { useSWRConfig } from "swr"; +import { ErrorContext } from "./App"; export default function StopGame() { + const { setError } = useContext(ErrorContext); const { mutate } = useSWRConfig(); - async function stopGame() { + async function onClick() { const res = await fetch("/api/party/stop", { method: "POST", }); if (!res.ok) { - throw new Error(await res.json()); + setError(await res.json()); + return; } mutate("/api/user"); } return ( - );