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

Allow Category tree on vtex legacy Filters and fixes url with the same page on Department Filters #179

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
30 changes: 29 additions & 1 deletion packs/vtex/loaders/legacy/productListingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import {
toProduct,
} from "deco-sites/std/packs/vtex/utils/transform.ts";
import { fetchAPI, fetchSafe } from "deco-sites/std/utils/fetchVTEX.ts";
import type { LegacyFacets, LegacyProduct } from "../../types.ts";

jovenan marked this conversation as resolved.
Show resolved Hide resolved
import type { LegacyFacet, LegacyFacets, LegacyProduct } from "../../types.ts";

import { withIsSimilarTo } from "../../utils/similars.ts";

const MAX_ALLOWED_PAGES = 500;
Expand Down Expand Up @@ -136,6 +138,7 @@ const loader = async (
const _to = `${(page + 1) * count - 1}`;

const pageTypes = await pageTypesFromPathname(maybeTerm, ctx);
const pageType = pageTypes.at(-1) || pageTypes[0];

if (pageTypes.length === 0 && !ft && !fq) {
return null;
Expand Down Expand Up @@ -182,8 +185,33 @@ const loader = async (
props.similars ? withIsSimilarTo(ctx, product) : product
),
);

// Get categories of the current department/category
const getCategoryFacets = (CategoriesTrees: LegacyFacet[]): LegacyFacet[] => {
const isDepartmentOrCategoryPage = !pageType;
if (isDepartmentOrCategoryPage) {
return [];
}

for (const category of CategoriesTrees) {
const isCurrentCategory = category.Id == Number(pageType.id);
if (isCurrentCategory) {
return category.Children || [];
} else if (category.Children.length) {
const childFacets = getCategoryFacets(category.Children);
const hasChildFacets = childFacets.length;
if (hasChildFacets) {
return childFacets;
}
}
}

return [];
};
jovenan marked this conversation as resolved.
Show resolved Hide resolved

const filters = Object.entries({
Departments: vtexFacets.Departments,
Categories: getCategoryFacets(vtexFacets.CategoriesTrees),
jovenan marked this conversation as resolved.
Show resolved Hide resolved
Brands: vtexFacets.Brands,
...vtexFacets.SpecificationFilters,
}).map(([name, facets]) =>
Expand Down
2 changes: 2 additions & 0 deletions packs/vtex/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ export type LegacyProduct = IProduct & {

export type LegacyFacets = {
Departments: LegacyFacet[];
CategoriesTrees: LegacyFacet[];
Brands: LegacyFacet[];
SpecificationFilters: Record<string, LegacyFacet[]>;
};
Expand Down Expand Up @@ -695,6 +696,7 @@ export interface Category {
}

export interface LegacyFacet {
Id: number;
Quantity: number;
Name: string;
Link: string;
Expand Down
5 changes: 0 additions & 5 deletions packs/vtex/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,6 @@ export const legacyFacetToFilter = (
const pathSet = new Set(pathSegments);

const getLink = (facet: LegacyFacet, selected: boolean) => {
// Do not allow removing root facet to avoid going back to home page
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this line causes the aforementioned bug

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tlgimenes I don't understand this comment, can you explain?
What would a fix for this look like?

this line of code in the verification makes all the returned filters have the wrong url when it is a department: https://australroupas.deco.site/masculino?O=OrderByScoreDESC

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 Down