mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-07-22 12:30:19 +08:00
Adding simple pagination to the admin user's view
This commit is contained in:
@@ -2,11 +2,21 @@ import { getToken } from "next-auth/jwt";
|
||||
import withRole from "src/lib/auth";
|
||||
import prisma from "src/lib/prismadb";
|
||||
|
||||
// The number of users to fetch in any request.
|
||||
const PAGE_SIZE = 20;
|
||||
|
||||
/**
|
||||
* Returns a list of user results from the database when the requesting user is
|
||||
* a logged in admin.
|
||||
*/
|
||||
const handler = withRole("admin", async (req, res) => {
|
||||
// Figure out the pagination index and skip that number of users.
|
||||
//
|
||||
// Note: with Prisma this isn't the most efficient but it's the only possible
|
||||
// option with cuid based User IDs.
|
||||
const { pageIndex } = req.query;
|
||||
const skip = pageIndex * PAGE_SIZE;
|
||||
|
||||
// Fetch 20 users.
|
||||
const users = await prisma.user.findMany({
|
||||
select: {
|
||||
@@ -15,7 +25,8 @@ const handler = withRole("admin", async (req, res) => {
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
take: 20,
|
||||
skip,
|
||||
take: PAGE_SIZE,
|
||||
});
|
||||
|
||||
res.status(200).json(users);
|
||||
|
||||
Reference in New Issue
Block a user