From 6cd7d164a22d709370c1a8f4005fe4c0a946f5a8 Mon Sep 17 00:00:00 2001 From: YulikK Date: Mon, 13 May 2024 18:59:27 +0200 Subject: [PATCH] feat: add filter Object, make filer with OR concatenation --- src/shared/API/product/utils/filter.ts | 85 ++++++++++++++++------- src/widgets/Catalog/model/CatalogModel.ts | 13 ++-- 2 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/shared/API/product/utils/filter.ts b/src/shared/API/product/utils/filter.ts index 34a6377a..3b0c8297 100644 --- a/src/shared/API/product/utils/filter.ts +++ b/src/shared/API/product/utils/filter.ts @@ -4,32 +4,63 @@ import type { FilterFieldsType, PriceRange } from '../../types/type.ts'; import { FilterFields } from '../../types/type.ts'; -export default function addFilter(field: FilterFieldsType, value?: PriceRange | string): string { - let result = ''; - switch (field) { - case FilterFields.CATEGORY: - if (typeof value === 'string') { - result = `${field}:subtree("${value}")`; - } - break; - case FilterFields.NEW_ARRIVAL: - result = field; - break; - case FilterFields.SALE: - result = field; - break; - case FilterFields.SIZE: - if (typeof value === 'string') { - result = `${field}:"${value}"`; - } - break; - case FilterFields.PRICE: - if (value && typeof value !== 'string') { - result = `${field}: range(${value.min * PRICE_FRACTIONS} to ${value.max * PRICE_FRACTIONS})`; - } - break; - default: - result = ''; +export default class FilterProduct { + private categories: string[] = []; + + private newArrival = ''; + + private price = ''; + + private sale = ''; + + private size: string[] = []; + + public addFilter(field: FilterFieldsType, value?: PriceRange | string): string[] { + switch (field) { + case FilterFields.CATEGORY: + if (typeof value === 'string') { + this.categories.push(value); + } + break; + case FilterFields.NEW_ARRIVAL: + this.newArrival = field; + break; + case FilterFields.SALE: + this.sale = field; + break; + case FilterFields.SIZE: + if (typeof value === 'string') { + this.size.push(value); + } + break; + case FilterFields.PRICE: + if (value && typeof value !== 'string') { + this.price = `${field}: range(${value.min * PRICE_FRACTIONS} to ${value.max * PRICE_FRACTIONS})`; + } + break; + default: + break; + } + return this.getFilter(); + } + + public getFilter(): string[] { + const result = []; + if (this.categories.length) { + result.push(`${FilterFields.CATEGORY}:${this.categories.map((category) => `subtree("${category}")`).join(',')}`); + } + if (this.size.length) { + result.push(`${FilterFields.SIZE}:${this.size.map((size) => `"${size}"`).join(',')}`); + } + if (this.newArrival) { + result.push(this.newArrival); + } + if (this.price) { + result.push(this.price); + } + if (this.sale) { + result.push(this.sale); + } + return result; } - return result; } diff --git a/src/widgets/Catalog/model/CatalogModel.ts b/src/widgets/Catalog/model/CatalogModel.ts index d67a2549..c8f25b90 100644 --- a/src/widgets/Catalog/model/CatalogModel.ts +++ b/src/widgets/Catalog/model/CatalogModel.ts @@ -3,7 +3,8 @@ import type ProductFiltersParams from '@/shared/types/productFilters.ts'; import ProductCardModel from '@/entities/ProductCard/model/ProductCardModel.ts'; import ProductFiltersModel from '@/features/ProductFilters/model/ProductFiltersModel.ts'; import getProductModel from '@/shared/API/product/model/ProductModel.ts'; -import addFilter from '@/shared/API/product/utils/filter.ts'; +// import addFilter from '@/shared/API/product/utils/filter.ts'; +import FilterProduct from '@/shared/API/product/utils/filter.ts'; import { FilterFields, type OptionsRequest } from '@/shared/API/types/type.ts'; import LoaderModel from '@/shared/Loader/model/LoaderModel.ts'; import getStore from '@/shared/Store/Store.ts'; @@ -55,16 +56,16 @@ class CatalogModel { private getSelectedFilters(): OptionsRequest { const { category, price, size } = getStore().getState().selectedFilters || {}; - const filter: OptionsRequest['filter'] = []; - category?.forEach((categoryID) => filter.push(addFilter(FilterFields.CATEGORY, categoryID))); + const filter = new FilterProduct(); + category?.forEach((categoryID) => filter.addFilter(FilterFields.CATEGORY, categoryID)); if (price?.max || price?.min) { - filter.push(addFilter(FilterFields.PRICE, price)); + filter.addFilter(FilterFields.PRICE, price); } if (size) { - filter.push(addFilter(FilterFields.SIZE, size)); + filter.addFilter(FilterFields.SIZE, size); } - return { filter, limit: 100, sort: { direction: 'desc', field: 'name', locale: 'en' } }; + return { filter: filter.getFilter(), limit: 100, sort: { direction: 'desc', field: 'name', locale: 'en' } }; } private init(): void {