Skip to content

Commit

Permalink
feat: add basic load more pagination for users
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Apr 10, 2023
1 parent 36c0f4e commit d4576f7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
3 changes: 2 additions & 1 deletion public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"update": "Update user",
"add": "Add user",
"saved": "Saved",
"save": "Save"
"save": "Save",
"more": "Load more"
}
}
4 changes: 2 additions & 2 deletions src/client/api/users.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import config from '../config';

export const getUsers = async () => {
const data = await fetch(`${config.get('api.host')}/admin/users`, {
export const getUsers = async (skip = 0) => {
const data = await fetch(`${config.get('api.host')}/admin/users?skip=${skip}`, {
method: 'GET',
cache: 'no-cache',
headers: {
Expand Down
18 changes: 18 additions & 0 deletions src/client/routes/account/users.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Alert,
ActionIcon,
Container,
Center,
Text,
Stack,
Select,
Expand Down Expand Up @@ -58,6 +59,7 @@ const addUserList = (users, data) => {
const Users = () => {
const [modalState, setModalState] = useState('add');
const [users, setUsers] = useState([]);
const [skip, setSkip] = useState(10);
const [success, setSuccess] = useState(false);
const [error, setError] = useState(null);
const [opened, { open, close }] = useDisclosure(false);
Expand Down Expand Up @@ -133,6 +135,16 @@ const Users = () => {
setUsers(updateUserList(users, { values: user }, 'delete'));
};

const onLoadUsers = async (event) => {
event.preventDefault();

const moreUsers = await getUsers(skip);

setSkip(skip + 20);

setUsers([...users, ...moreUsers]);
};

const openDeleteModal = (user) =>
openConfirmModal({
title: 'Delete ' + user.username,
Expand Down Expand Up @@ -260,6 +272,12 @@ const Users = () => {
</Table>
</Group>

<Center>
<Button color="hemmelig-orange" onClick={onLoadUsers}>
{t('users.more')}
</Button>
</Center>

<Group position="right">
<Button
leftIcon={<IconPlus size={14} />}
Expand Down
19 changes: 17 additions & 2 deletions src/server/controllers/admin/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,23 @@ async function users(fastify) {
{
preValidation: [fastify.authenticate, fastify.admin],
},
async () => {
const users = await prisma.user.findMany({});
async (request, reply) => {
const { skip = 0, take = 10 } = request.query;

if (take > 100) {
return reply.code(403).send({
type: 'take',
error: 'Not allowed to fetch more users than 100.',
});
}

const users = await prisma.user.findMany({
skip: Number(skip),
take: Number(take),
orderBy: {
role: 'asc',
},
});

return getRedactedUsers(users);
}
Expand Down

0 comments on commit d4576f7

Please sign in to comment.