Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 27 additions & 1 deletion vtex/loaders/legacy/productListingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "../../utils/segment.ts";
import { withIsSimilarTo } from "../../utils/similars.ts";
import { legacyFacetToFilter, toProduct } from "../../utils/transform.ts";
import type { LegacyProduct, LegacySort } from "../../utils/types.ts";
import type { LegacyProduct, LegacySort, LegacyFacet } from "../../utils/types.ts";

const MAX_ALLOWED_PAGES = 500;

Expand Down Expand Up @@ -129,6 +129,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 @@ -177,8 +178,33 @@ const loader = async (
props.similars ? withIsSimilarTo(req, ctx, product) : product
),
);

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

Choose a reason for hiding this comment

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

Is this condition accurate?

Copy link
Contributor Author

@jovenan jovenan Sep 21, 2023

Choose a reason for hiding this comment

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

yes, because if the page is not a departament or category, an error will occur. For example if the page is a search page, the pageType const will be undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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 [];
};

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

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

export interface LegacyFacet {
Id: number;
Quantity: number;
Name: string;
Link: string;
Expand Down