Skip to content

Commit

Permalink
feat(#1300722): [Source Field] Manage Geopoint Attribute - skateboard
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-goupil committed Jul 24, 2024
1 parent bdc68f8 commit bd94619
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 12 deletions.
1 change: 1 addition & 0 deletions front/example-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@mui/material": "5.10.6",
"@mui/x-data-grid": "5.17.4",
"classnames": "2.3.2",
"date-fns": "3.6.0",
"lodash.debounce": "4.0.8",
"react": "18.x",
"react-dom": "18.x",
Expand Down
18 changes: 18 additions & 0 deletions front/example-app/src/components/Facets/Facet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ function Facet(props: IProps): JSX.Element {
onChange={onChange}
/>
)
case AggregationType.HISTOGRAM:
return (
<FacetLoadMore
filter={filter}
id={id}
loadMore={loadMore}
moreOptions={moreOptions}
renderOption={(option): JSX.Element => (
<FacetChoice
key={String(option.value)}
activeOptions={activeOptions}
filter={filter}
onChange={onChange}
option={option}
/>
)}
/>
)

default:
return (
Expand Down
10 changes: 9 additions & 1 deletion front/example-app/src/components/Facets/Facets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ function Facets(props: IProps): JSX.Element {
const filterIndex = prevState.findIndex(
(activeFilter) => activeFilter.filter.field === filter.field
)

if (filterIndex !== -1) {
clone.splice(filterIndex, 1, { filter, value })
// For the following type, if the value is the same from the previous, we remove the filter from the array
if (
prevState[filterIndex].value === value
) {
clone.splice(filterIndex, 1)
} else {
clone.splice(filterIndex, 1, { filter, value })
}
return clone
}
}
Expand Down
9 changes: 6 additions & 3 deletions front/example-app/src/hooks/useProducts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function useProducts(
const [sort, sortOrder, sortOptions, setSort, setSortOrder] = useProductSort()
const [activeFilters, setActiveFilters] = useState<IActiveFilters>([])
const [moreOptions, setMoreOptions] = useState<IFilterMoreOptions>(new Map())
const queryFilters: IProductFieldFilterInput = useMemo(
const queryFilters: IProductFieldFilterInput[] = useMemo(
() => getProductFilters(activeFilters),
[activeFilters]
)
Expand Down Expand Up @@ -111,15 +111,18 @@ export function useProducts(
aggregation: filter.field,
localizedCatalog: String(localizedCatalogId),
}
const categoryIdFilter = queryFilters.find(
(value) => Object.keys(value)[0] === 'category__id'
)
if (search) {
variables.search = search
}
if (currentCategoryId) {
variables.currentCategoryId = currentCategoryId
}
if (queryFilters.category__id) {
if (categoryIdFilter) {
variables.currentCategoryId = (
queryFilters.category__id as ICategoryTypeDefaultFilterInputType
categoryIdFilter.category_id as ICategoryTypeDefaultFilterInputType
).eq
}
graphqlApi<IGraphqlViewMoreProductFacetOptions>(
Expand Down
89 changes: 84 additions & 5 deletions front/example-app/src/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,30 @@ import {
} from '@elastic-suite/gally-admin-shared'

import { IActiveFilters } from '../types'
import { add, format, parse } from 'date-fns'

type DurationKey = 'Y' | 'M' | 'W' | 'D' | 'H' | 'm' | 'S'

const durationFormat: Record<DurationKey, string> = {
Y: 'years',
M: 'months',
W: 'weeks',
D: 'days',
H: 'hours',
m: 'minutes',
S: 'seconds',
}

/* eslint-disable no-underscore-dangle */
export function getProductFilters(
activeFilters: IActiveFilters
): IProductFieldFilterInput {
return activeFilters.reduce<IProductFieldFilterInput>((acc, activeFilter) => {
): IProductFieldFilterInput[] {
const data: IProductFieldFilterInput[] = []
activeFilters.forEach((activeFilter) => {
const precAcc = data.find(
(value) => Object.keys(value)[0] === activeFilter.filter.field
)
const acc: IProductFieldFilterInput = precAcc || {}
if (activeFilter.filter.type === AggregationType.CATEGORY) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
eq: activeFilter.value,
Expand All @@ -36,7 +55,67 @@ 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()
)
// TODO: use default_date_range_interval in graphql endpoint
const incrementString = activeFilter.filter.default_date_range_interval || '1Y'
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<string, number> = {}

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 acc
}, {})
}
if (!precAcc) data.push(acc)
})

return data
}
Loading

0 comments on commit bd94619

Please sign in to comment.