️ Use infinite query on polls page (#1200)

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Luke Vella 2024-07-10 08:34:00 +01:00 committed by GitHub
parent e985a5db9f
commit 43c450aa91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 182 additions and 66 deletions

View file

@ -93,6 +93,58 @@ export const polls = router({
},
});
}),
infiniteList: possiblyPublicProcedure
.input(
z.object({
status: z.enum(["all", "live", "paused", "finalized"]),
cursor: z.string().optional(),
limit: z.number(),
}),
)
.query(async ({ ctx, input }) => {
const { cursor, limit, status } = input;
const polls = await prisma.poll.findMany({
where: {
userId: ctx.user.id,
status: status === "all" ? undefined : status,
},
orderBy: [
{
createdAt: "desc",
},
{
title: "asc",
},
],
cursor: cursor ? { id: cursor } : undefined,
take: limit + 1,
select: {
id: true,
title: true,
location: true,
timeZone: true,
createdAt: true,
status: true,
userId: true,
participants: {
select: {
id: true,
name: true,
},
},
},
});
let nextCursor: typeof cursor | undefined = undefined;
if (polls.length > input.limit) {
const nextItem = polls.pop();
nextCursor = nextItem!.id;
}
return {
polls,
nextCursor,
};
}),
// START LEGACY ROUTES
create: possiblyPublicProcedure