Skip to content

Commit

Permalink
[Shopify] Fix pagination loader PLP (#894)
Browse files Browse the repository at this point in the history
* fix(shopify): pagination loader plp

* refactor(shopify): add page off set loader plp
  • Loading branch information
luanargolodev authored Oct 16, 2024
1 parent abee205 commit 75165bd
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions shopify/loaders/ProductListingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") || "";
Expand Down Expand Up @@ -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",
Expand All @@ -204,7 +214,7 @@ const loader = async (
pageInfo: {
nextPage: hasNextPage ? `?${nextPage}` : undefined,
previousPage: hasPreviousPage ? `?${previousPage}` : undefined,
currentPage: page,
currentPage,
records,
recordPerPage: count,
},
Expand Down

0 comments on commit 75165bd

Please sign in to comment.