Implement paging in manage users, implement search

This commit is contained in:
Manuel
2023-07-31 22:57:22 +02:00
parent 35d505e9b1
commit d2408ebe4b
3 changed files with 80 additions and 40 deletions

View File

@@ -161,23 +161,33 @@ export const userRouter = createTRPCRouter({
getAll: publicProcedure
.input(
z.object({
limit: z.number().min(1).max(100).nullish(),
cursor: z.string().nullish(),
limit: z.number().min(1).max(100).default(10),
page: z.number().min(0),
search: z
.string()
.optional()
.transform((value) => (value === '' ? undefined : value)),
})
)
.query(async ({ ctx, input }) => {
const limit = input.limit ?? 50;
const cursor = input.cursor;
const limit = input.limit;
const users = await ctx.prisma.user.findMany({
take: limit + 1, // get an extra item at the end which we'll use as next cursor
cursor: cursor ? { id: cursor } : undefined,
take: limit + 1,
skip: limit * input.page,
where: {
name: {
contains: input.search,
},
},
});
let nextCursor: typeof cursor | undefined = undefined;
if (users.length > limit) {
const nextItem = users.pop();
nextCursor = nextItem!.id;
}
const countUsers = await ctx.prisma.user.count({
where: {
name: {
contains: input.search,
},
},
});
return {
users: users.map((user) => ({
@@ -186,7 +196,7 @@ export const userRouter = createTRPCRouter({
email: user.email,
emailVerified: user.emailVerified,
})),
nextCursor,
countPages: Math.ceil(countUsers / limit),
};
}),
createUser: publicProcedure.input(createNewUserSchema).mutation(async ({ ctx, input }) => {