diff --git a/front/example-app/src/services/product.ts b/front/example-app/src/services/product.ts index 584eccd6d..ca559efd9 100644 --- a/front/example-app/src/services/product.ts +++ b/front/example-app/src/services/product.ts @@ -8,15 +8,6 @@ import { } from '@elastic-suite/gally-admin-shared' import { IActiveFilters } from '../types' -import { add, format, parse } from 'date-fns' - -type DurationKey = 'y' | 'M' | 'd' - -const durationFormat: Record = { - y: 'years', - M: 'months', - d: 'days', -} /* eslint-disable no-underscore-dangle */ export function getProductFilters( @@ -44,7 +35,10 @@ export function getProductFilters( acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = { lte: Number(activeFilter.value), } as IEntityIntegerTypeFilterInput - } else if (activeFilter.filter.type === AggregationType.CHECKBOX) { + } else if (activeFilter.filter.type === AggregationType.CHECKBOX + || activeFilter.filter.type === AggregationType.HISTOGRAM_DATE + || activeFilter.filter.type === AggregationType.HISTOGRAM + ) { if (!(activeFilter.filter.field in acc)) { acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = { in: [], @@ -55,66 +49,6 @@ export function getProductFilters( activeFilter.filter.field as keyof IProductFieldFilterInput ] as ISelectTypeDefaultFilterInputType ).in.push(activeFilter.value) - } else if (activeFilter.filter.type === AggregationType.HISTOGRAM_DATE) { - if (!acc.boolFilter?._should) { - acc.boolFilter = { _should: [] } - } - const field = activeFilter.filter - .field as keyof IProductFieldFilterInput - const date = parse( - activeFilter.value, - activeFilter.filter.date_format, - new Date() - ) - - const incrementString = activeFilter.filter.date_range_interval - const incrementNumber = Number( - incrementString.substring(0, incrementString.length - 1) - ) - const incrementType = incrementString.substring( - incrementString.length - 1 - ) as DurationKey - - const gt = format(date, activeFilter.filter.date_format) - const lt = format( - add(date, { - [durationFormat[incrementType]]: incrementNumber, - }), - activeFilter.filter.date_format - ) - - acc.boolFilter._should.push({ - [field]: { - gt, - lt, - }, - }) - } else if (activeFilter.filter.type === AggregationType.HISTOGRAM) { - if (!acc.boolFilter?._should) { - acc.boolFilter = { _should: [] } - } - const field = activeFilter.filter - .field as keyof IProductFieldFilterInput - const arrayValue = activeFilter.value.split('-') - const [firstValue, secondValue] = arrayValue - - if (arrayValue.length > 1) { - const isFirstValueIsAsterisk = firstValue === '*' - const isSecondValueIsAsterisk = secondValue === '*' - let data: Record = {} - - if (isFirstValueIsAsterisk && !isSecondValueIsAsterisk) { - data = { lt: Number(secondValue) } - } else if (!isFirstValueIsAsterisk && !isSecondValueIsAsterisk) { - data = { gte: Number(firstValue), lte: Number(secondValue) } - } else { - data = { gt: Number(firstValue) } - } - - acc.boolFilter._should.push({ - [field]: data, - }) - } } return data