Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-adaptio authored Oct 29, 2023
2 parents 7b4dc33 + 10de01a commit 1089e17
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
26 changes: 21 additions & 5 deletions algolia/loaders/product/listingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ interface Props {

/** @description Hide Unavailable Items */
hideUnavailable?: boolean;

/**
* @description ?page search params for the first page
* @default 0
*/
startingPage?: 0 | 1;
}

const getPageInfo = (
Expand All @@ -53,6 +59,7 @@ const getPageInfo = (
nbHits: number,
hitsPerPage: number,
url: URL,
startingPage: number,
) => {
const next = page + 1;
const prev = page - 1;
Expand All @@ -62,19 +69,19 @@ const getPageInfo = (
const previousPage = new URLSearchParams(url.searchParams);

if (hasNextPage) {
nextPage.set("page", `${next}`);
nextPage.set("page", `${next + startingPage}`);
}

if (hasPreviousPage) {
previousPage.set("page", `${prev}`);
previousPage.set("page", `${prev + startingPage}`);
}

return {
nextPage: hasNextPage ? `?${nextPage}` : undefined,
previousPage: hasPreviousPage ? `?${previousPage}` : undefined,
records: nbHits,
recordPerPage: hitsPerPage,
currentPage: page,
currentPage: page + startingPage,
};
};

Expand Down Expand Up @@ -168,6 +175,8 @@ const loader = async (
const url = new URL(req.url);
const { client } = ctx;
const indexName = getIndex(url.searchParams.get("sort"));
const startingPage = props.startingPage ?? 0;
const pageIndex = Number(url.searchParams.get("page")) || startingPage;

const facetFilters: [string, string[]][] = JSON.parse(
url.searchParams.get("facetFilters") ?? "[]",
Expand Down Expand Up @@ -195,7 +204,7 @@ const loader = async (
filters: fFilters,
facets: [],
hitsPerPage: props.hitsPerPage ?? 12,
page: Number(url.searchParams.get("page")) || 0,
page: pageIndex - startingPage,
clickAnalytics: true,
},
},
Expand Down Expand Up @@ -226,7 +235,14 @@ const loader = async (
client,
{ url, queryID, indexName },
);
const pageInfo = getPageInfo(page, nbPages, nbHits, hitsPerPage, url);
const pageInfo = getPageInfo(
page,
nbPages,
nbHits,
hitsPerPage,
url,
startingPage,
);
const filters = transformFacets(facets ?? {}, {
order: props.facets ?? [],
facetFilters,
Expand Down
4 changes: 3 additions & 1 deletion vtex/loaders/legacy/productListingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ const loader = async (
const pageType = pageTypes.at(-1) || pageTypes[0];

const missingParams = typeof maybeMap !== "string" || !maybeTerm;
const [map, term] = missingParams
const [map, term] = missingParams && fq.length > 0
? ["", ""]
: missingParams
? getMapAndTerm(pageTypes)
: [maybeMap, maybeTerm];

Expand Down

0 comments on commit 1089e17

Please sign in to comment.