diff --git a/app/leaderboard/page.tsx b/app/leaderboard/page.tsx index 3e9156b..98ddb27 100644 --- a/app/leaderboard/page.tsx +++ b/app/leaderboard/page.tsx @@ -1,8 +1,10 @@ +import LeaderboardRow from "@/components/LeaderboardRow"; import prisma from "@/lib/prisma"; async function getLeaderboardUsers() { return await prisma.user.findMany({ select: { + id: true, name: true, kills: true, deaths: true, @@ -26,13 +28,7 @@ export default async function Leaderboard() { const users = await getLeaderboardUsers(); const rows = users.map((user, i) => ( - - {i + 1} - {user.name} - {user.kills} - {user.deaths} - {user.wins} - + )); return ( diff --git a/app/user/[id]/page.tsx b/app/user/[id]/page.tsx new file mode 100644 index 0000000..10f071a --- /dev/null +++ b/app/user/[id]/page.tsx @@ -0,0 +1,120 @@ +import prisma from "@/lib/prisma"; + +export default async function UserById({ params }: { params: { id: string } }) { + const id = params.id; + const user = await prisma.user.findUnique({ where: { id } }); + + if (!user) { + return

No user found with id {id}

; + } + + return ( + <> +

{user.name}

+

{user.id}

+
+
+
+ + + + + + + +
+
Date Joined
+
+ {user.createdAt.toLocaleDateString()} +
+
+
+
+ + + + + + + + + +
+
Wins
+
{user.wins}
+
+
+
+ + + + + + + +
+
Kills
+
{user.kills}
+
+
+
+ + + + + + + + +
+
Deaths
+
{user.deaths}
+
+
+ + ); +} diff --git a/components/LeaderboardRow.tsx b/components/LeaderboardRow.tsx new file mode 100644 index 0000000..14fed9e --- /dev/null +++ b/components/LeaderboardRow.tsx @@ -0,0 +1,28 @@ +"use client"; + +import { LeaderboardUser } from "@/lib/user"; +import { useRouter } from "next/navigation"; + +export default function LeaderboardRow({ + user, + rank, +}: { + user: LeaderboardUser; + rank: number; +}) { + const router = useRouter(); + + async function onClick() { + router.push(`/user/${user.id}`); + } + + return ( + onClick()}> + {rank} + {user.name} + {user.kills} + {user.deaths} + {user.wins} + + ); +} diff --git a/lib/user.ts b/lib/user.ts index bfefbcf..6be1e2b 100644 --- a/lib/user.ts +++ b/lib/user.ts @@ -18,3 +18,29 @@ export async function getUser(email: string) { }, }); } + +export type LeaderboardUsers = Awaited>; +export type LeaderboardUser = LeaderboardUsers[0]; + +async function getLeaderboardUsers() { + return await prisma.user.findMany({ + select: { + id: true, + name: true, + kills: true, + deaths: true, + wins: true, + }, + orderBy: [ + { + wins: "desc", + }, + { + kills: "desc", + }, + { + deaths: "asc", + }, + ], + }); +}