Skip to content

Commit

Permalink
add mutates
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 24, 2024
1 parent d70fd7b commit 29d415f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions components/CreateParty.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"use client";

import { useSWRConfig } from "swr";

export default function CreateParty() {
const { mutate } = useSWRConfig();

async function createParty() {
const res = await fetch("/api/party", {
method: "POST",
Expand All @@ -9,6 +13,8 @@ export default function CreateParty() {
if (!res.ok) {
throw new Error(await res.json());
}

mutate("/api/user");
}

return (
Expand Down
5 changes: 5 additions & 0 deletions components/JoinParty.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"use client";

import { FormEvent } from "react";
import { useSWRConfig } from "swr";

export default function JoinParty() {
const { mutate } = useSWRConfig();

async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();

Expand All @@ -18,6 +21,8 @@ export default function JoinParty() {
if (!res.ok) {
throw new Error(await res.json());
}

mutate("/api/user");
}

return (
Expand Down
2 changes: 1 addition & 1 deletion components/Party.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function Party() {
<h2>Mode: {_.startCase(_.toLower(party.mode))}</h2>
<h2>Players:</h2>
<ul>{players}</ul>
{party.adminId === user.id && <StartGame />}
<StartGame visible={party.adminId === user.id} />
<LeaveParty />
</div>
);
Expand Down
6 changes: 5 additions & 1 deletion components/StartGame.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"use client";

export default function StartGame() {
export default function StartGame({ visible }: { visible: boolean }) {
if (!visible) {
return;
}

async function startGame() {
const res = await fetch("/api/party/start", {
method: "POST",
Expand Down

0 comments on commit 29d415f

Please sign in to comment.