Skip to content

Commit eca06f5

Browse files
committed
feat(admin): fix pagination issue
1 parent 3ba050a commit eca06f5

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

components/Admin/Transfer/List.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import Link from "next/link";
77
const AdminTransferList: React.FC<{
88
transfers: ITransfer[];
99
total: number;
10-
}> = ({ transfers, total }) => {
10+
onPageChange: (page: number) => void;
11+
pageSize: number;
12+
}> = ({ transfers, total, onPageChange, pageSize }) => {
1113
return (
1214
<DataGrid
1315
columns={[
@@ -27,8 +29,9 @@ const AdminTransferList: React.FC<{
2729
rows={transfers ?? []}
2830
rowCount={total}
2931
paginationMode="server"
30-
pageSize={10}
32+
pageSize={pageSize}
3133
pagination
34+
onPageChange={onPageChange}
3235
/>
3336
);
3437
};

pages/admin/index.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@ type Props = {
1212
user: SessionUser;
1313
transfers: ITransfer[];
1414
total: number;
15+
pageSize: number;
1516
};
1617

17-
const AdminPage: NextPage<Props> = ({ user, transfers, total }) => {
18-
const { refresh } = useRouter();
18+
const AdminPage: NextPage<Props> = ({ user, transfers, total, pageSize }) => {
19+
const { refresh, push } = useRouter();
1920

2021
const onLogout = () => {
2122
fetch("/api/admin/logout").then((res) => {
2223
res.ok && refresh();
2324
});
2425
};
2526

27+
const onPageChange = (page: number) => {
28+
const query = new URLSearchParams(window.location.search);
29+
query.set("page", page.toString());
30+
push(`/admin?${query.toString()}`);
31+
};
32+
2633
return (
2734
<Container
2835
sx={{ py: 4, height: "100vh", display: "flex", flexDirection: "column" }}
@@ -42,7 +49,12 @@ const AdminPage: NextPage<Props> = ({ user, transfers, total }) => {
4249
border: "px solid #000",
4350
}}
4451
>
45-
<AdminTransferList transfers={transfers} total={total} />
52+
<AdminTransferList
53+
transfers={transfers}
54+
total={total}
55+
onPageChange={onPageChange}
56+
pageSize={pageSize}
57+
/>
4658
</Box>
4759
</Container>
4860
);
@@ -65,14 +77,16 @@ export const getServerSideProps: GetServerSideProps = withSessionSsr(
6577
await dbConnect();
6678

6779
// Get query params
68-
const { id } = query;
80+
const { id, page } = query;
6981
const filters: FilterQuery<ITransfer> = {};
7082
if (id) {
7183
filters.id = id as string;
7284
}
85+
const pageSize = 10;
7386
const transfers = await TransferModel.find(filters)
7487
.sort({ createdAt: -1 })
75-
.limit(10);
88+
.skip((Number(page) || 0) * pageSize)
89+
.limit(pageSize);
7690

7791
const total = await TransferModel.countDocuments();
7892

@@ -82,6 +96,7 @@ export const getServerSideProps: GetServerSideProps = withSessionSsr(
8296
JSON.parse(JSON.stringify(transfer))
8397
),
8498
total,
99+
pageSize,
85100
};
86101

87102
return {

0 commit comments

Comments
 (0)