From c1e531c7be3c3b0dadd2fe283586cc4d6c77bfc7 Mon Sep 17 00:00:00 2001 From: jovenan Date: Fri, 15 Sep 2023 22:47:42 -0300 Subject: [PATCH] feat: Allow category tree filters on vtex legacy filters --- vtex/loaders/legacy/productListingPage.ts | 28 ++++++++++++++++++++++- vtex/utils/types.ts | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/vtex/loaders/legacy/productListingPage.ts b/vtex/loaders/legacy/productListingPage.ts index ef24475d7..f80270580 100644 --- a/vtex/loaders/legacy/productListingPage.ts +++ b/vtex/loaders/legacy/productListingPage.ts @@ -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; @@ -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; @@ -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; + 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]) => diff --git a/vtex/utils/types.ts b/vtex/utils/types.ts index bf5fa957e..a42b05d52 100644 --- a/vtex/utils/types.ts +++ b/vtex/utils/types.ts @@ -659,6 +659,7 @@ export type LegacyProduct = IProduct & { export type LegacyFacets = { Departments: LegacyFacet[]; + CategoriesTrees: LegacyFacet[]; Brands: LegacyFacet[]; SpecificationFilters: Record; }; @@ -693,6 +694,7 @@ export interface Category { } export interface LegacyFacet { + Id: number; Quantity: number; Name: string; Link: string;