Skip to content

Commit 0c2a7fe

Browse files
authored
Task 573 next previous buttons (#103)
Demo of changes: ![PrevNextPR](https://github.com/HacktheHill/track-the-hack/assets/95104238/6e7964eb-7b64-463c-9655-65bdc246a11d) Changes: Added new hacker routers to get the next and previous hacker from a unique id (in the url) using cursor-based pagination. Buttons are just preliminary and should be changed based on desired design later
1 parent 8cec4b5 commit 0c2a7fe

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

src/pages/hackers/hacker.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const Hacker: NextPage = () => {
2727

2828
const hackerQuery = trpc.hackers.get.useQuery({ id: id ?? "" }, { enabled: !!id });
2929
const presenceQuery = trpc.presence.getFromHackerId.useQuery({ id: id ?? "" }, { enabled: !!id });
30+
31+
const nextHackerQuery = trpc.hackers.getNext.useQuery({ id: id ?? "" }, { enabled: !!id });
32+
const prevHackerQuery = trpc.hackers.getPrev.useQuery({ id: id ?? "" }, { enabled: !!id });
3033

3134
if (hackerQuery.isLoading || hackerQuery.data == null) {
3235
return (
@@ -82,6 +85,24 @@ const Hacker: NextPage = () => {
8285
>
8386
<div className="mx-auto flex max-w-2xl flex-col gap-4">
8487
<OnlyRole filter={role => role === Role.ORGANIZER || role === Role.SPONSOR}>
88+
<div className="flex justify-between">
89+
{prevHackerQuery.data ? (<a
90+
href={`/hackers/hacker?id=${prevHackerQuery.data.id}`}
91+
className="flex items-center justify-center gap-2 rounded-md bg-gray-800 px-4 py-2 text-white hover:bg-gray-700"
92+
>
93+
Previous
94+
</a>
95+
) : (
96+
<a></a>
97+
)}
98+
{nextHackerQuery.data && ( <a
99+
href={`/hackers/hacker?id=${nextHackerQuery.data.id}`}
100+
className="flex items-center justify-center gap-2 rounded-md bg-gray-800 px-4 py-2 text-white hover:bg-gray-700"
101+
>
102+
Next
103+
</a>
104+
)}
105+
</div>
85106
<HackerView hackerData={hackerQuery.data} presenceData={presenceQuery.data} />
86107
</OnlyRole>
87108
<OnlyRole filter={role => role === Role.HACKER}>{t("not-authorized-to-view-this-page")}</OnlyRole>

src/server/api/routers/hackers.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,60 @@ export const hackerRouter = createTRPCRouter({
4242

4343
return hacker;
4444
}),
45+
46+
// Get next hacker in db from an id
47+
getNext: publicProcedure
48+
.input(
49+
z
50+
.object({
51+
id: z.string(),
52+
}),
53+
)
54+
.query(async ({ ctx, input }) => {
55+
let hacker: HackerInfo | null = null;
56+
if ("id" in input) {
57+
hacker = await ctx.prisma.hackerInfo.findFirst({
58+
take: 1,
59+
skip: 1,
60+
cursor: {
61+
id: input.id,
62+
},
63+
});
64+
}
65+
66+
if (!hacker) {
67+
throw new Error("Hacker not found");
68+
}
69+
70+
return hacker;
71+
}),
72+
73+
// Get prev hacker in db from an id
74+
getPrev: publicProcedure
75+
.input(
76+
z
77+
.object({
78+
id: z.string(),
79+
}),
80+
)
81+
.query(async ({ ctx, input }) => {
82+
let hacker: HackerInfo | null = null;
83+
if ("id" in input) {
84+
hacker = await ctx.prisma.hackerInfo.findFirst({
85+
take: -1,
86+
skip: 1,
87+
cursor: {
88+
id: input.id,
89+
},
90+
});
91+
}
92+
93+
if (!hacker) {
94+
throw new Error("Hacker not found");
95+
}
96+
97+
return hacker;
98+
}),
4599

46100
// Get all hackers
47101
all: protectedProcedure

0 commit comments

Comments
 (0)