From f284516a59af8f02e6f4b5ea7566cbfa356d6c6d Mon Sep 17 00:00:00 2001 From: jovenan Date: Sat, 5 Aug 2023 17:44:16 -0300 Subject: [PATCH 1/7] feat: Allow Category tree on vtex legacy Filters --- .../vtex/loaders/legacy/productListingPage.ts | 29 ++++++++++++++++++- packs/vtex/types.ts | 1 + packs/vtex/utils/transform.ts | 6 ++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/packs/vtex/loaders/legacy/productListingPage.ts b/packs/vtex/loaders/legacy/productListingPage.ts index f31fd570..23c8ef1b 100644 --- a/packs/vtex/loaders/legacy/productListingPage.ts +++ b/packs/vtex/loaders/legacy/productListingPage.ts @@ -22,7 +22,7 @@ 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"; +import type { LegacyFacet, LegacyFacets, LegacyProduct } from "../../types.ts"; const MAX_ALLOWED_PAGES = 500; @@ -172,8 +172,35 @@ const loader = async ( priceCurrency: config!.defaultPriceCurrency, }) ); + + const terms = term.split("/"); + + const getCategoryFacets = (CategoriesTrees: LegacyFacet[]) => { + const category = CategoriesTrees.find((category) => { + if (category.Link.toLowerCase().includes(term.toLowerCase())) { + return category; + } else if ( + map.includes("specificationFilter") && + category.Link.toLowerCase().indexOf( + terms[terms.length - 2].toLowerCase(), + ) + ) { + return category; + } else if (category.Children.length) { + getCategoryFacets(category.Children); + } + }); + + if (category) { + return category.Children; + } else { + return []; + } + }; + const filters = Object.entries({ Departments: vtexFacets.Departments, + Categories: getCategoryFacets(vtexFacets.CategoriesTrees), Brands: vtexFacets.Brands, ...vtexFacets.SpecificationFilters, }).map(([name, facets]) => diff --git a/packs/vtex/types.ts b/packs/vtex/types.ts index 9215fdb1..057137c9 100644 --- a/packs/vtex/types.ts +++ b/packs/vtex/types.ts @@ -661,6 +661,7 @@ export type LegacyProduct = IProduct & { export type LegacyFacets = { Departments: LegacyFacet[]; + CategoriesTrees: LegacyFacet[]; Brands: LegacyFacet[]; SpecificationFilters: Record; }; diff --git a/packs/vtex/utils/transform.ts b/packs/vtex/utils/transform.ts index 8121daed..e9ddf467 100644 --- a/packs/vtex/utils/transform.ts +++ b/packs/vtex/utils/transform.ts @@ -448,9 +448,9 @@ export const legacyFacetToFilter = ( 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}`; - } + // if (mapSegments.length === 1) { + // return `${url.pathname}${url.search}`; + // } const index = pathSegments.findIndex((s) => s === facet.Value); const newMap = selected From f83e9421399bf5d15184d04bd43ea2846fe61f05 Mon Sep 17 00:00:00 2001 From: jovenan Date: Mon, 7 Aug 2023 21:38:45 -0300 Subject: [PATCH 2/7] fix: getCategoryFacets with the category Id verification --- .../vtex/loaders/legacy/productListingPage.ts | 31 ++++++++++--------- packs/vtex/types.ts | 1 + 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packs/vtex/loaders/legacy/productListingPage.ts b/packs/vtex/loaders/legacy/productListingPage.ts index 23c8ef1b..37a3a8ef 100644 --- a/packs/vtex/loaders/legacy/productListingPage.ts +++ b/packs/vtex/loaders/legacy/productListingPage.ts @@ -130,6 +130,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; @@ -173,29 +174,29 @@ const loader = async ( }) ); - const terms = term.split("/"); - + // Get categories of the current department/category const getCategoryFacets = (CategoriesTrees: LegacyFacet[]) => { + // Verifies if the page is a department or category page + if (!pageType) { + return []; + } + const category = CategoriesTrees.find((category) => { - if (category.Link.toLowerCase().includes(term.toLowerCase())) { - return category; - } else if ( - map.includes("specificationFilter") && - category.Link.toLowerCase().indexOf( - terms[terms.length - 2].toLowerCase(), - ) - ) { + // Verifies if the category Id is the same of the atual page category Id + // If true return the category + // Else, verifies if category has a children and calls the function to verify the next level of the category + if (category.Id == Number(pageType.id)) { return category; } else if (category.Children.length) { getCategoryFacets(category.Children); } + + // if does not match with any category, return null + return null; }); - if (category) { - return category.Children; - } else { - return []; - } + // return the children of the current department/category + return category?.Children || []; }; const filters = Object.entries({ diff --git a/packs/vtex/types.ts b/packs/vtex/types.ts index 057137c9..34dc0b63 100644 --- a/packs/vtex/types.ts +++ b/packs/vtex/types.ts @@ -696,6 +696,7 @@ export interface Category { } export interface LegacyFacet { + Id: number; Quantity: number; Name: string; Link: string; From 9c443aaafb624e9d6ddcc690e705d4fddc8c0d9b Mon Sep 17 00:00:00 2001 From: jovenan Date: Tue, 8 Aug 2023 14:33:29 -0300 Subject: [PATCH 3/7] fix: renaming variables and removing comments --- packs/vtex/loaders/legacy/productListingPage.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/packs/vtex/loaders/legacy/productListingPage.ts b/packs/vtex/loaders/legacy/productListingPage.ts index c1a47c64..4550012c 100644 --- a/packs/vtex/loaders/legacy/productListingPage.ts +++ b/packs/vtex/loaders/legacy/productListingPage.ts @@ -27,7 +27,6 @@ import type { LegacyFacet, LegacyFacets, LegacyProduct } from "../../types.ts"; import { withIsSimilarTo } from "../../utils/similars.ts"; - const MAX_ALLOWED_PAGES = 500; export interface Props { @@ -189,26 +188,22 @@ const loader = async ( // Get categories of the current department/category const getCategoryFacets = (CategoriesTrees: LegacyFacet[]) => { - // Verifies if the page is a department or category page - if (!pageType) { + const isDepartmentOrCategoryPage = !pageType; + if (isDepartmentOrCategoryPage) { return []; } const category = CategoriesTrees.find((category) => { - // Verifies if the category Id is the same of the atual page category Id - // If true return the category - // Else, verifies if category has a children and calls the function to verify the next level of the category - if (category.Id == Number(pageType.id)) { + const isCurrentCategory = category.Id == Number(pageType.id); + if (isCurrentCategory) { return category; } else if (category.Children.length) { getCategoryFacets(category.Children); } - // if does not match with any category, return null return null; }); - // return the children of the current department/category return category?.Children || []; }; From 9c099150d8afd9196559a53b68340d40dfd260d7 Mon Sep 17 00:00:00 2001 From: jovenan Date: Wed, 9 Aug 2023 01:20:44 -0300 Subject: [PATCH 4/7] fix: Remove recursion of getCategoryFacets --- .../vtex/loaders/legacy/productListingPage.ts | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/packs/vtex/loaders/legacy/productListingPage.ts b/packs/vtex/loaders/legacy/productListingPage.ts index 4550012c..3c14b413 100644 --- a/packs/vtex/loaders/legacy/productListingPage.ts +++ b/packs/vtex/loaders/legacy/productListingPage.ts @@ -193,18 +193,27 @@ const loader = async ( return []; } - const category = CategoriesTrees.find((category) => { - const isCurrentCategory = category.Id == Number(pageType.id); - if (isCurrentCategory) { - return category; - } else if (category.Children.length) { - getCategoryFacets(category.Children); - } + const stack = [...CategoriesTrees]; + + while (stack.length > 0) { + const currentCategory = stack.pop(); + + if (currentCategory) { + const isCurrentCategory = currentCategory.Id == Number(pageType.id); + if (isCurrentCategory) { + return currentCategory.Children || []; + } - return null; - }); + const hasChildren = currentCategory.Children.length; + + if (hasChildren) { + const addChildrenToVerification = [...currentCategory.Children]; + stack.push(...addChildrenToVerification); + } + } + } - return category?.Children || []; + return []; }; const filters = Object.entries({ From f1690d765e814951987d652830a44bcff4fad4d7 Mon Sep 17 00:00:00 2001 From: jovenan Date: Wed, 9 Aug 2023 14:44:55 -0300 Subject: [PATCH 5/7] fix: using recursion to getCategoryFacets --- .../vtex/loaders/legacy/productListingPage.ts | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/packs/vtex/loaders/legacy/productListingPage.ts b/packs/vtex/loaders/legacy/productListingPage.ts index 3c14b413..e29bf84b 100644 --- a/packs/vtex/loaders/legacy/productListingPage.ts +++ b/packs/vtex/loaders/legacy/productListingPage.ts @@ -187,28 +187,21 @@ const loader = async ( ); // Get categories of the current department/category - const getCategoryFacets = (CategoriesTrees: LegacyFacet[]) => { + const getCategoryFacets = (CategoriesTrees: LegacyFacet[]): LegacyFacet[] => { const isDepartmentOrCategoryPage = !pageType; if (isDepartmentOrCategoryPage) { return []; } - const stack = [...CategoriesTrees]; - - while (stack.length > 0) { - const currentCategory = stack.pop(); - - if (currentCategory) { - const isCurrentCategory = currentCategory.Id == Number(pageType.id); - if (isCurrentCategory) { - return currentCategory.Children || []; - } - - const hasChildren = currentCategory.Children.length; - - if (hasChildren) { - const addChildrenToVerification = [...currentCategory.Children]; - stack.push(...addChildrenToVerification); + 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; } } } From 191b4e5da6d2e0582a1848cd3bfbf3d7510ff166 Mon Sep 17 00:00:00 2001 From: jovenan Date: Thu, 10 Aug 2023 13:56:56 -0300 Subject: [PATCH 6/7] fix: Remove code coments --- packs/vtex/utils/transform.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packs/vtex/utils/transform.ts b/packs/vtex/utils/transform.ts index e9ddf467..e3554432 100644 --- a/packs/vtex/utils/transform.ts +++ b/packs/vtex/utils/transform.ts @@ -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 - // 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)] From 9bfb0a94d5ed684f89b6207872e047b3197239d5 Mon Sep 17 00:00:00 2001 From: Jonatas Venancio <49959786+jovenan@users.noreply.github.com> Date: Fri, 11 Aug 2023 00:36:25 -0300 Subject: [PATCH 7/7] fix: Remove Break row Co-authored-by: Tiago Gimenes --- packs/vtex/loaders/legacy/productListingPage.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packs/vtex/loaders/legacy/productListingPage.ts b/packs/vtex/loaders/legacy/productListingPage.ts index e29bf84b..354ca35e 100644 --- a/packs/vtex/loaders/legacy/productListingPage.ts +++ b/packs/vtex/loaders/legacy/productListingPage.ts @@ -22,7 +22,6 @@ import { toProduct, } from "deco-sites/std/packs/vtex/utils/transform.ts"; import { fetchAPI, fetchSafe } from "deco-sites/std/utils/fetchVTEX.ts"; - import type { LegacyFacet, LegacyFacets, LegacyProduct } from "../../types.ts"; import { withIsSimilarTo } from "../../utils/similars.ts";