'use client'; import { ArrowLeftIcon } from '@heroicons/react/24/outline'; import { ArrowRightIcon } from '@heroicons/react/24/outline'; import { createUrl } from 'lib/utils'; import { usePathname, useSearchParams, useRouter } from 'next/navigation'; export default function Pagination({ itemsPerPage, itemsTotal, currentPage }: { itemsPerPage: number; itemsTotal: number; currentPage: number; }) { const router = useRouter(); const pathname = usePathname(); const currentParams = useSearchParams(); const q = currentParams.get('q'); const sort = currentParams.get('sort'); const pageCount = Math.ceil(itemsTotal / itemsPerPage); // Invoke when user click to request another page. test const handlePageClick = (page: number) => { const newPage = Math.ceil(page + 1); let newUrl = createUrl( pathname, new URLSearchParams({ ...(q && { q }), ...(sort && { sort }) }) ); if (page !== 0) { newUrl = createUrl( pathname, new URLSearchParams({ ...(q && { q }), ...(sort && { sort }), page: newPage.toString() }) ); } router.replace(newUrl); }; return (
); }