Skip to content

Commit

Permalink
use onClick
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 27, 2024
1 parent 68d5b71 commit 6d74c17
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions components/CreateParty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
Expand All @@ -18,7 +18,7 @@ export default function CreateParty() {
}

return (
<button className="btn btn-primary" onClick={() => createParty()}>
<button className="btn btn-primary" onClick={() => onClick()}>
Create Party
</button>
);
Expand Down
4 changes: 2 additions & 2 deletions components/LeaveParty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -22,7 +22,7 @@ export default function LeaveParty() {
}

return (
<button className="btn btn-error" onClick={() => leaveParty()}>
<button className="btn btn-error" onClick={() => onClick()}>
Leave Party
{isLoading && <span className="loading loading-dots loading-sm"></span>}
</button>
Expand Down
4 changes: 2 additions & 2 deletions components/StartGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
Expand All @@ -22,7 +22,7 @@ export default function StartGame() {
}

return (
<button className="btn btn-success" onClick={() => startGame()}>
<button className="btn btn-success" onClick={() => onClick()}>
Start Game
</button>
);
Expand Down
10 changes: 7 additions & 3 deletions components/StopGame.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<button className="btn btn-warning" onClick={() => stopGame()}>
<button className="btn btn-warning" onClick={() => onClick()}>
Stop Game
</button>
);
Expand Down

0 comments on commit 6d74c17

Please sign in to comment.