Skip to content

Commit

Permalink
add swr
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 24, 2024
1 parent 090d14e commit d70fd7b
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 156 deletions.
47 changes: 0 additions & 47 deletions app/api/party/join/[id]/route.ts

This file was deleted.

4 changes: 2 additions & 2 deletions app/api/party/join/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export async function POST(req: Request) {
return Response.json(null, { status: 401 });
}

const form = await req.formData();
const code = form.get("code")?.toString().toLowerCase();
const data = await req.json();
const code = data.code;

let party = await prisma.party.findUnique({
where: {
Expand Down
21 changes: 13 additions & 8 deletions app/api/user/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth";

export async function GET() {
const session = await getServerSession();
if (!session?.user?.email) {
return Response.json(null, { status: 401 });
}

const user = await prisma.user.findUniqueOrThrow({
async function getUser(email: string) {
return await prisma.user.findUniqueOrThrow({
where: {
email: session.user.email,
email,
},
include: {
target: true,
Expand All @@ -20,6 +15,16 @@ export async function GET() {
},
},
});
}

export type User = Awaited<ReturnType<typeof getUser>>;

export async function GET() {
const session = await getServerSession();
if (!session?.user?.email) {
return Response.json(null, { status: 401 });
}

const user = await getUser(session.user.email);
return Response.json(user);
}
6 changes: 0 additions & 6 deletions components/CreateParty.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use client";

import { useRouter } from "next/navigation";

export default function CreateParty() {
const router = useRouter();

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

router.refresh();
}

return (
Expand Down
62 changes: 15 additions & 47 deletions components/JoinParty.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,27 @@
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth";
import { revalidatePath } from "next/cache";
"use client";

export default function JoinParty() {
async function joinParty(form: FormData) {
"use server";

const session = await getServerSession();
if (!session?.user?.email) {
return Response.json(null, { status: 401 });
}

const code = form.get("code")?.toString().toLowerCase();
import { FormEvent } from "react";

let party = await prisma.party.findUnique({
where: {
code,
export default function JoinParty() {
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();

const data = new FormData(event.currentTarget);
const res = await fetch("/api/party/join", {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({ code: data.get("code") }),
});

if (!party) {
return Response.json(`Failed to find party with code ${code}`, {
status: 400,
});
}

if (party.started) {
return Response.json(`Cannot join party that has already started`, {
status: 403,
});
if (!res.ok) {
throw new Error(await res.json());
}

await prisma.user.update({
where: {
email: session.user.email,
},
data: {
partyId: party.id,
},
});

party = await prisma.party.findUnique({
where: {
code,
},
include: {
players: true,
},
});

revalidatePath("/");
}

return (
<form action={joinParty}>
<form onSubmit={onSubmit}>
<label htmlFor="code">Party Code:</label>
<input
type="text"
Expand Down
6 changes: 0 additions & 6 deletions components/LeaveParty.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use client";

import { useRouter } from "next/navigation";

export default function LeaveParty() {
const router = useRouter();

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

router.refresh();
}

return (
Expand Down
39 changes: 14 additions & 25 deletions components/Party.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth";
"use client";

import _ from "lodash";
import LeaveParty from "./LeaveParty";
import CreateParty from "./CreateParty";
Expand All @@ -8,34 +8,23 @@ import StartGame from "./StartGame";
import { Party } from "@prisma/client";
import AdminBadge from "./AdminBadge";
import PartyStarted from "./PartyStarted";
import useSWR, { Fetcher } from "swr";
import { User } from "@/app/api/user/route";
import Spinner from "./Spinner";

export type User = Awaited<ReturnType<typeof getUser>>;

async function getUser() {
const session = await getServerSession();
if (!session?.user?.email) {
return;
}
export default function Party() {
const fetcher: Fetcher<User, string> = (url) =>
fetch(url).then((res) => res.json());

const user = await prisma.user.findUniqueOrThrow({
where: {
email: session.user.email,
},
include: {
party: {
include: {
players: true,
},
},
target: true,
},
const { data, error, isLoading } = useSWR("/api/user", fetcher, {
refreshInterval: 500,
});

return user;
}
if (isLoading) {
return <Spinner />;
}

export default async function Party() {
const user = await getUser();
const user = data;
if (!user) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion components/PartyStarted.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import LeaveParty from "./LeaveParty";
import { User } from "./Party";
import StopGame from "./StopGame";
import { User } from "@/app/api/user/route";

export default function PartyStarted({ user }: { user: User }) {
if (!user) {
Expand Down
23 changes: 23 additions & 0 deletions components/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default function Spinner() {
return (
<div role="status">
<svg
aria-hidden="true"
className="w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
);
}
6 changes: 0 additions & 6 deletions components/StartGame.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use client";

import { useRouter } from "next/navigation";

export default function StartGame() {
const router = useRouter();

async function startGame() {
const res = await fetch("/api/party/start", {
method: "POST",
Expand All @@ -15,8 +11,6 @@ export default function StartGame() {
}

console.log(await res.json());

router.refresh();
}

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

import { useRouter } from "next/navigation";

export default function StopGame() {
const router = useRouter();

async function stopGame() {
const res = await fetch("/api/party/stop", {
method: "POST",
Expand All @@ -15,8 +11,6 @@ export default function StopGame() {
}

console.log(await res.json());

router.refresh();
}

return (
Expand Down
23 changes: 22 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"next": "14.1.0",
"next-auth": "^4.24.6",
"react": "^18",
"react-dom": "^18"
"react-dom": "^18",
"swr": "^2.2.5"
},
"devDependencies": {
"@types/lodash": "^4.14.202",
Expand Down

0 comments on commit d70fd7b

Please sign in to comment.