🐛 Fix pagination on register token page

This commit is contained in:
Manuel
2023-07-31 20:52:17 +02:00
parent 129fd1336c
commit 71757d0cce
2 changed files with 49 additions and 21 deletions

View File

@@ -5,16 +5,34 @@ import dayjs from 'dayjs';
import Head from 'next/head'; import Head from 'next/head';
import { useState } from 'react'; import { useState } from 'react';
import { MainLayout } from '~/components/layout/admin/main-admin.layout'; import { MainLayout } from '~/components/layout/admin/main-admin.layout';
import { modals as applicationModals } from '~/modals/modals';
import { api } from '~/utils/api'; import { api } from '~/utils/api';
const ManageUserInvitesPage = () => { const ManageUserInvitesPage = () => {
const { data, isFetched, fetchPreviousPage, fetchNextPage } = const { data, fetchPreviousPage, fetchNextPage, hasNextPage, hasPreviousPage } =
api.registrationTokens.getAllInvites.useInfiniteQuery({ api.registrationTokens.getAllInvites.useInfiniteQuery(
limit: 10, {
}); limit: 10,
},
{
getNextPageParam: (lastPage) => lastPage.nextCursor,
}
);
const [activePage, _] = useState(0); const [activePage, setActivePage] = useState(0);
const handleFetchNextPage = async () => {
await fetchNextPage();
setActivePage((prev) => prev + 1);
};
const handleFetchPreviousPage = async () => {
await fetchPreviousPage();
setActivePage((prev) => prev - 1);
};
const currentPage = data?.pages[activePage];
console.log(data?.pages);
return ( return (
<MainLayout> <MainLayout>
@@ -44,7 +62,10 @@ const ManageUserInvitesPage = () => {
</Button> </Button>
</Flex> </Flex>
{data && ( {activePage}
{currentPage ? 'current page is defined' : 'current page is not defined'}
{data && currentPage && (
<> <>
<Table mb="md" withBorder highlightOnHover> <Table mb="md" withBorder highlightOnHover>
<thead> <thead>
@@ -55,7 +76,7 @@ const ManageUserInvitesPage = () => {
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{data.pages[activePage].registrationTokens.map((token, index) => ( {currentPage.registrationTokens.map((token, index) => (
<tr key={index}> <tr key={index}>
<td> <td>
<Text>{token.id}</Text> <Text>{token.id}</Text>
@@ -68,21 +89,25 @@ const ManageUserInvitesPage = () => {
)} )}
</td> </td>
<td> <td>
<ActionIcon onClick={() => { <ActionIcon
modals.openContextModal({ onClick={() => {
modal: 'deleteRegistrationTokenModal', modals.openContextModal({
title: <Text weight="bold">Delete registration token</Text>, modal: 'deleteRegistrationTokenModal',
innerProps: { title: <Text weight="bold">Delete registration token</Text>,
tokenId: token.id, innerProps: {
} tokenId: token.id,
}) },
}} color="red" variant="light"> });
}}
color="red"
variant="light"
>
<IconTrash size="1rem" /> <IconTrash size="1rem" />
</ActionIcon> </ActionIcon>
</td> </td>
</tr> </tr>
))} ))}
{data.pages[activePage].registrationTokens.length === 0 && ( {currentPage.registrationTokens.length === 0 && (
<tr> <tr>
<td colSpan={3}> <td colSpan={3}>
<Center p="md"> <Center p="md">
@@ -94,10 +119,10 @@ const ManageUserInvitesPage = () => {
</tbody> </tbody>
</Table> </Table>
<Pagination <Pagination
total={data.pages.length} total={data.pages[0].countPages}
value={activePage + 1} value={activePage + 1}
onNextPage={fetchNextPage} onNextPage={handleFetchNextPage}
onPreviousPage={fetchPreviousPage} onPreviousPage={handleFetchPreviousPage}
/> />
</> </>
)} )}

View File

@@ -26,12 +26,15 @@ export const inviteRouter = createTRPCRouter({
nextCursor = nextItem!.id; nextCursor = nextItem!.id;
} }
const countRegistrationTokens = await ctx.prisma.registrationToken.count();
return { return {
registrationTokens: registrationTokens.map((token) => ({ registrationTokens: registrationTokens.map((token) => ({
id: token.id, id: token.id,
expires: token.expires, expires: token.expires,
})), })),
nextCursor, nextCursor,
countPages: Math.ceil(countRegistrationTokens / limit)
}; };
}), }),
createRegistrationToken: publicProcedure createRegistrationToken: publicProcedure