From 84935c2fe8e00fa4dcb9eb131c0a080974148faf Mon Sep 17 00:00:00 2001 From: Luan Lemos Date: Tue, 1 Oct 2024 21:02:26 -0300 Subject: [PATCH 1/2] fix(shopify): pagination loader plp --- shopify/loaders/ProductListingPage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shopify/loaders/ProductListingPage.ts b/shopify/loaders/ProductListingPage.ts index bf2ed07a8..5f04b3bb3 100644 --- a/shopify/loaders/ProductListingPage.ts +++ b/shopify/loaders/ProductListingPage.ts @@ -73,7 +73,7 @@ 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 page = props.page || Number(url.searchParams.get("page")) || 1; const endCursor = props.endCursor || url.searchParams.get("endCursor") || ""; const startCursor = props.startCursor || url.searchParams.get("startCursor") || ""; From 1e9f31d52952db71bdf1f8c85cd4d0d132559098 Mon Sep 17 00:00:00 2001 From: Luan Lemos Date: Fri, 4 Oct 2024 17:07:03 -0300 Subject: [PATCH 2/2] 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 5f04b3bb3..005319dc7 100644 --- a/shopify/loaders/ProductListingPage.ts +++ b/shopify/loaders/ProductListingPage.ts @@ -37,6 +37,11 @@ export interface Props { * @description number of products per page to display */ count: number; + /** + * @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 @@ -73,7 +78,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")) || 1; + 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") || ""; @@ -157,18 +166,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", @@ -188,7 +198,7 @@ const loader = async ( pageInfo: { nextPage: hasNextPage ? `?${nextPage}` : undefined, previousPage: hasPreviousPage ? `?${previousPage}` : undefined, - currentPage: page, + currentPage, records, recordPerPage: count, },