Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shopify] Fix pagination loader PLP #894

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -169,18 +178,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 @@ -200,7 +210,7 @@ const loader = async (
pageInfo: {
nextPage: hasNextPage ? `?${nextPage}` : undefined,
previousPage: hasPreviousPage ? `?${previousPage}` : undefined,
currentPage: page,
currentPage,
records,
recordPerPage: count,
},
Expand Down
Loading