Skip to content

Commit

Permalink
Merge pull request #41 from deco-cx/fix_filter_on_legacy_search_and_c…
Browse files Browse the repository at this point in the history
…ategories

Add missing VTEX catalog filters URL on categories and search
  • Loading branch information
matheusgr authored Sep 13, 2023
2 parents a292127 + ad778cd commit b6a7dca
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
12 changes: 5 additions & 7 deletions vtex/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,20 +515,14 @@ export const legacyFacetToFilter = (
map: string,
behavior: "dynamic" | "static",
): Filter | null => {
const mapSegments = map.split(",");
const mapSegments = map.split(",").filter((x) => x.length > 0);
const pathSegments = url.pathname
.replace(/^\//, "")
.split("/")
.slice(0, mapSegments.length);
const mapSet = new Set(mapSegments);
const pathSet = new Set(pathSegments);

const getLink = (facet: LegacyFacet, selected: boolean) => {
// Do not allow removing root facet to avoid going back to home page
if (mapSegments.length === 1) {
return `${url.pathname}${url.search}`;
}

const index = pathSegments.findIndex((s) => s === facet.Value);
const newMap = selected
? [...mapSegments.filter((_, i) => i !== index)]
Expand All @@ -542,6 +536,10 @@ export const legacyFacetToFilter = (
if (behavior === "static") {
link.searchParams.set("fmap", url.searchParams.get("fmap") || map);
}
const currentQuery = url.searchParams.get("q");
if (currentQuery) {
link.searchParams.set("q", currentQuery);
}

return `${link.pathname}${link.search}`;
};
Expand Down
1 change: 1 addition & 0 deletions website/components/_Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare global {
LIVE: {
page: Page;
site: Site;
flags?: Flag[];
};
}
}
Expand Down

0 comments on commit b6a7dca

Please sign in to comment.