diff --git a/packages/nextjs/components/Pagination.tsx b/packages/nextjs/components/Pagination.tsx index cb9e2d7..3ecda9b 100644 --- a/packages/nextjs/components/Pagination.tsx +++ b/packages/nextjs/components/Pagination.tsx @@ -1,9 +1,7 @@ import * as React from "react"; +import { Pagination as BasePagination } from "shared-ui"; import Link from "next/link"; -import { useRouter } from "next/router"; -import { FaChevronLeft, FaChevronRight } from "react-icons/fa"; import classNames from "classnames"; -import { motion } from "framer-motion"; type PaginationProps = { pageCount: number; @@ -12,19 +10,6 @@ type PaginationProps = { }; export const Pagination = ({ onPageChange, pageCount = 1, currentPage = 1 }: PaginationProps) => { - const router = useRouter(); - const { page: _page, ...otherQuery } = router.query; - const baseUrlObj = { - pathname: router.pathname, - query: otherQuery, - }; - const getUrlObjWithPage = (pageNum: number) => ({ - ...baseUrlObj, - query: { ...baseUrlObj.query, page: pageNum.toString() }, - }); - - const totalPages = Array.from({ length: pageCount }, (_item, index) => index + 1); - const handlePageChanged = (e: React.MouseEvent, page: number) => { if (onPageChange) { e.preventDefault(); @@ -32,68 +17,25 @@ export const Pagination = ({ onPageChange, pageCount = 1, currentPage = 1 }: Pag } }; - const prevUrlObj = currentPage <= 2 ? baseUrlObj : getUrlObjWithPage(currentPage - 1); - const nextUrlObj = currentPage >= pageCount ? baseUrlObj : getUrlObjWithPage(currentPage + 1); - - return ( - - - -
- {totalPages.map((page) => ( - - handlePageChanged(e, page)} - className={classNames( - "border rounded w-10 aspect-square flex items-center justify-center", - page === currentPage ? "border-primary" : "border-[transparent]" - )} - aria-current={page === currentPage ? "page" : "false"} - > - {page} - - - ))} -
- = pageCount} - className="inline-flex items-center gap-2 leading-none" - > - Next - -
- ); -}; - -const MaybeDisabledLink = ({ - isDisabled, - urlObject, - className, - children, -}: React.PropsWithChildren<{ - isDisabled?: boolean; - className?: string; - urlObject: any; -}>) => { - if (!isDisabled) { - return ( - - {children} - - ); - } - return ( - - {children} - + ( + + handlePageChanged(e, page)} + className={classNames( + "border rounded w-10 aspect-square flex items-center justify-center", + page === currentPage ? "border-primary" : "border-[transparent]" + )} + aria-current={page === currentPage ? "page" : "false"} + > + {page} + + + )} + /> ); }; diff --git a/packages/shared-ui/components/Pagination.tsx b/packages/shared-ui/components/Pagination.tsx new file mode 100644 index 0000000..19b98f5 --- /dev/null +++ b/packages/shared-ui/components/Pagination.tsx @@ -0,0 +1,87 @@ +import * as React from "react"; +import { FaChevronLeft, FaChevronRight } from "react-icons/fa"; +import classNames from "classnames"; +import { motion } from "framer-motion"; + +type PaginationProps = { + pageCount: number; + currentPage?: number; + onPageChange?: (page: number) => void; + NextPreviousLink: React.ElementType; + renderPaginationLink: ({ page, href }: { page: number; href: string }) => JSX.Element; +}; + +export const Pagination = ({ + pageCount = 1, + currentPage = 1, + renderPaginationLink, + NextPreviousLink, +}: PaginationProps) => { + const url = new URL(window.location.href); + const params = new URLSearchParams(url.search); + const getUrlWithPage = (pageNum: number) => { + params.set("page", pageNum.toString()); + return `${url.pathname}?${params}`; + }; + + const totalPages = Array.from({ length: pageCount }, (_item, index) => index + 1); + + const prevUrl = currentPage <= 2 ? url.toString() : getUrlWithPage(currentPage - 1); + const nextUrl = currentPage >= pageCount ? url.toString() : getUrlWithPage(currentPage + 1); + + return ( + + + +
+ {totalPages.map((page) => + renderPaginationLink({ page, href: page === 1 ? url.toString() : getUrlWithPage(page) }) + )} +
+ = pageCount} + className="inline-flex items-center gap-2 leading-none" + > + Next + +
+ ); +}; + +const MaybeDisabledLink = ({ + isDisabled, + href, + className, + children, + as = "a", +}: React.PropsWithChildren<{ + isDisabled?: boolean; + className?: string; + href: string; + as?: React.ElementType; +}>) => { + if (!isDisabled) { + const Link = as; + return ( + + {children} + + ); + } + + return ( + + {children} + + ); +}; diff --git a/packages/shared-ui/index.ts b/packages/shared-ui/index.ts index af904c9..9df6467 100644 --- a/packages/shared-ui/index.ts +++ b/packages/shared-ui/index.ts @@ -13,6 +13,7 @@ export * from "./components/WeDontSellBreadBanner"; export * from "./components/Price"; export * from "./components/QuantityInput"; export * from './components/ProductSort'; +export * from './components/Pagination'; export * from "./components/sanity"; export * from "./components/cart";