Skip to content

Commit

Permalink
add confirm and deny kill
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 26, 2024
1 parent 8e28d50 commit 542f774
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
29 changes: 29 additions & 0 deletions components/ConfirmKill.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<button className="btn btn-success" onClick={() => onClick()}>
Confirm Kill
</button>
);
}
29 changes: 29 additions & 0 deletions components/DenyKill.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<button className="btn btn-error" onClick={() => onClick()}>
Deny Kill
</button>
);
}
23 changes: 17 additions & 6 deletions components/KillTarget.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<button className="btn btn-accent">
Pending
<span className="loading loading-dots loading-sm"></span>
</button>
);
}

return (
<button className="btn btn-accent" onClick={() => killTarget()}>
<button className="btn btn-accent" onClick={() => onClick()}>
Kill Target
</button>
);
Expand Down
18 changes: 16 additions & 2 deletions components/Party.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<User, string> = (url) =>
fetch(url).then((res) => res.json());

Expand Down Expand Up @@ -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 (
<PartyCard code={party.code}>
<h2>Target: {user.target.name}</h2>
<div className="card-actions justify-center">
<KillTarget />
{user.pending ? (
<>
<ConfirmKill />
<DenyKill />
</>
) : (
<KillTarget pending={user.target.pending} />
)}
{party.adminId === user.id && <StopGame />}
<LeaveParty />
</div>
Expand Down

0 comments on commit 542f774

Please sign in to comment.