From 75165bde408390f77531809778b7a2ee27fa6cff Mon Sep 17 00:00:00 2001 From: Luan Argolo Lemos Date: Wed, 16 Oct 2024 08:51:41 -0300 Subject: [PATCH] [Shopify] Fix pagination loader PLP (#894) * fix(shopify): pagination loader plp * refactor(shopify): add page off set loader plp --- shopify/loaders/ProductListingPage.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/shopify/loaders/ProductListingPage.ts b/shopify/loaders/ProductListingPage.ts index 11820c768..f1a8f563b 100644 --- a/shopify/loaders/ProductListingPage.ts +++ b/shopify/loaders/ProductListingPage.ts @@ -44,6 +44,11 @@ export interface Props { * @description search for metafields */ metafields?: Metafield[]; + /** + * @title Starting page query parameter offset. + * @description Set the starting page offset. Default to 1. + */ + pageOffset?: number; /** * @hide * @description it is hidden because only page prop is not sufficient, we need cursors @@ -80,7 +85,11 @@ const loader = async ( const count = props.count ?? 12; const query = props.query || url.searchParams.get("q") || ""; - const page = props.page || Number(url.searchParams.get("page")) || 0; + const currentPageoffset = props.pageOffset ?? 1; + const pageParam = url.searchParams.get("page") + ? Number(url.searchParams.get("page")) - currentPageoffset + : 0; + const page = props.page || pageParam; const endCursor = props.endCursor || url.searchParams.get("endCursor") || ""; const startCursor = props.startCursor || url.searchParams.get("startCursor") || ""; @@ -173,18 +182,19 @@ const loader = async ( const previousPage = new URLSearchParams(url.searchParams); if (hasNextPage) { - nextPage.set("page", (page + 1).toString()); + nextPage.set("page", (page + currentPageoffset + 1).toString()); nextPage.set("startCursor", shopifyProducts?.pageInfo.endCursor ?? ""); nextPage.delete("endCursor"); } if (hasPreviousPage) { - previousPage.set("page", (page - 1).toString()); + previousPage.set("page", (page + currentPageoffset - 1).toString()); previousPage.set("endCursor", shopifyProducts?.pageInfo.startCursor ?? ""); previousPage.delete("startCursor"); } const filters = shopifyFilters?.map((filter) => toFilter(filter, url)); + const currentPage = page + currentPageoffset; return { "@type": "ProductListingPage", @@ -204,7 +214,7 @@ const loader = async ( pageInfo: { nextPage: hasNextPage ? `?${nextPage}` : undefined, previousPage: hasPreviousPage ? `?${previousPage}` : undefined, - currentPage: page, + currentPage, records, recordPerPage: count, },