Skip to content

Commit

Permalink
feat: add filter Object, make filer with OR concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
YulikK committed May 13, 2024
1 parent 0df859f commit 6cd7d16
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 33 deletions.
85 changes: 58 additions & 27 deletions src/shared/API/product/utils/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 7 additions & 6 deletions src/widgets/Catalog/model/CatalogModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6cd7d16

Please sign in to comment.