Add pagination to events page (#1689)

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Luke Vella 2025-04-24 15:30:30 +01:00 committed by GitHub
parent 9adebe496b
commit 8d2e5f8359
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 82 additions and 41 deletions

View file

@ -1,3 +1,4 @@
import type { Prisma } from "@rallly/database";
import { prisma } from "@rallly/database";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
@ -19,55 +20,65 @@ export async function getScheduledEvents({
userId,
status,
search,
page = 1,
pageSize = 10,
}: {
userId: string;
status: Status;
search?: string;
page?: number;
pageSize?: number;
}) {
const now = new Date();
const rawEvents = await prisma.scheduledEvent.findMany({
where: {
userId,
deletedAt: null,
...(status != "past" && { start: { gte: now } }),
...(status === "past" && { start: { lt: now } }),
...(search && { title: { contains: search, mode: "insensitive" } }),
status: mapStatus[status],
},
orderBy: {
start: status === "past" ? "desc" : "asc",
},
select: {
id: true,
title: true,
description: true,
location: true,
start: true,
end: true,
allDay: true,
timeZone: true,
status: true,
invites: {
select: {
id: true,
inviteeName: true,
user: {
select: {
image: true,
const where: Prisma.ScheduledEventWhereInput = {
userId,
deletedAt: null,
...(status != "past" && { start: { gte: now } }),
...(status === "past" && { start: { lt: now } }),
...(search && { title: { contains: search, mode: "insensitive" } }),
status: mapStatus[status],
};
const [rawEvents, totalCount] = await Promise.all([
prisma.scheduledEvent.findMany({
where,
orderBy: {
start: status === "past" ? "desc" : "asc",
},
select: {
id: true,
title: true,
description: true,
location: true,
start: true,
end: true,
allDay: true,
timeZone: true,
status: true,
invites: {
select: {
id: true,
inviteeName: true,
user: {
select: {
image: true,
},
},
},
},
},
},
});
skip: (page - 1) * pageSize,
take: pageSize,
}),
prisma.scheduledEvent.count({ where }),
]);
const events = rawEvents.map((event) => ({
...event,
status:
event.status === "confirmed"
? // If the event is confirmed, it's either past or upcoming
((event.start < now ? "past" : "upcoming") as Status)
? ((event.start < now ? "past" : "upcoming") as Status)
: event.status,
invites: event.invites.map((invite) => ({
id: invite.id,
@ -76,5 +87,8 @@ export async function getScheduledEvents({
})),
}));
return events;
const totalPages = Math.ceil(totalCount / pageSize);
const hasNextPage = page * pageSize < totalCount;
return { events, totalCount, totalPages, hasNextPage };
}