Chapter 11 - Implement pagination

This commit is contained in:
2024-01-03 08:51:37 +01:00
parent 4f2ab25f16
commit 86036bdf1f
2 changed files with 19 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ import { CreateInvoice } from '@/app/ui/invoices/buttons';
import { lusitana } from '@/app/ui/fonts'; import { lusitana } from '@/app/ui/fonts';
import { InvoicesTableSkeleton } from '@/app/ui/skeletons'; import { InvoicesTableSkeleton } from '@/app/ui/skeletons';
import { Suspense } from 'react'; import { Suspense } from 'react';
import {fetchInvoicesPages} from "@/app/lib/data";
export default async function Page({ export default async function Page({
searchParams, searchParams,
@@ -15,7 +16,10 @@ export default async function Page({
}; };
}) { }) {
const query = searchParams?.query || ''; const query = searchParams?.query || '';
const currentPage = Number(searchParams?.page) || 1; return ( const currentPage = Number(searchParams?.page) || 1;
const totalPages = await fetchInvoicesPages(query);
return (
<div className="w-full"> <div className="w-full">
<div className="flex w-full items-center justify-between"> <div className="flex w-full items-center justify-between">
<h1 className={`${lusitana.className} text-2xl`}>Invoices</h1> <h1 className={`${lusitana.className} text-2xl`}>Invoices</h1>
@@ -28,7 +32,7 @@ export default async function Page({
<Table query={query} currentPage={currentPage} /> <Table query={query} currentPage={currentPage} />
</Suspense> </Suspense>
<div className="mt-5 flex w-full justify-center"> <div className="mt-5 flex w-full justify-center">
{/* <Pagination totalPages={totalPages} /> */} <Pagination totalPages={totalPages} />
</div> </div>
</div> </div>
); );

View File

@@ -4,17 +4,27 @@ import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline';
import clsx from 'clsx'; import clsx from 'clsx';
import Link from 'next/link'; import Link from 'next/link';
import { generatePagination } from '@/app/lib/utils'; import { generatePagination } from '@/app/lib/utils';
import { usePathname, useSearchParams } from 'next/navigation';
export default function Pagination({ totalPages }: { totalPages: number }) { export default function Pagination({ totalPages }: { totalPages: number }) {
// NOTE: comment in this code when you get to this point in the course // NOTE: comment in this code when you get to this point in the course
const pathname = usePathname();
const searchParams = useSearchParams();
const currentPage = Number(searchParams.get('page')) || 1;
// const allPages = generatePagination(currentPage, totalPages); const createPageURL = (pageNumber: number | string) => {
const params = new URLSearchParams(searchParams);
params.set('page', pageNumber.toString());
return `${pathname}?${params.toString()}`;
};
const allPages = generatePagination(currentPage, totalPages);
return ( return (
<> <>
{/* NOTE: comment in this code when you get to this point in the course */} {/* NOTE: comment in this code when you get to this point in the course */}
{/* <div className="inline-flex"> <div className="inline-flex">
<PaginationArrow <PaginationArrow
direction="left" direction="left"
href={createPageURL(currentPage - 1)} href={createPageURL(currentPage - 1)}
@@ -47,7 +57,7 @@ export default function Pagination({ totalPages }: { totalPages: number }) {
href={createPageURL(currentPage + 1)} href={createPageURL(currentPage + 1)}
isDisabled={currentPage >= totalPages} isDisabled={currentPage >= totalPages}
/> />
</div> */} </div>
</> </>
); );
} }