Skip to content

Commit bd94619

Browse files
feat(#1300722): [Source Field] Manage Geopoint Attribute - skateboard
1 parent bdc68f8 commit bd94619

File tree

7 files changed

+256
-12
lines changed

7 files changed

+256
-12
lines changed

front/example-app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@mui/material": "5.10.6",
1111
"@mui/x-data-grid": "5.17.4",
1212
"classnames": "2.3.2",
13+
"date-fns": "3.6.0",
1314
"lodash.debounce": "4.0.8",
1415
"react": "18.x",
1516
"react-dom": "18.x",

front/example-app/src/components/Facets/Facet.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ function Facet(props: IProps): JSX.Element {
101101
onChange={onChange}
102102
/>
103103
)
104+
case AggregationType.HISTOGRAM:
105+
return (
106+
<FacetLoadMore
107+
filter={filter}
108+
id={id}
109+
loadMore={loadMore}
110+
moreOptions={moreOptions}
111+
renderOption={(option): JSX.Element => (
112+
<FacetChoice
113+
key={String(option.value)}
114+
activeOptions={activeOptions}
115+
filter={filter}
116+
onChange={onChange}
117+
option={option}
118+
/>
119+
)}
120+
/>
121+
)
104122

105123
default:
106124
return (

front/example-app/src/components/Facets/Facets.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,16 @@ function Facets(props: IProps): JSX.Element {
4747
const filterIndex = prevState.findIndex(
4848
(activeFilter) => activeFilter.filter.field === filter.field
4949
)
50+
5051
if (filterIndex !== -1) {
51-
clone.splice(filterIndex, 1, { filter, value })
52+
// For the following type, if the value is the same from the previous, we remove the filter from the array
53+
if (
54+
prevState[filterIndex].value === value
55+
) {
56+
clone.splice(filterIndex, 1)
57+
} else {
58+
clone.splice(filterIndex, 1, { filter, value })
59+
}
5260
return clone
5361
}
5462
}

front/example-app/src/hooks/useProducts.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function useProducts(
3636
const [sort, sortOrder, sortOptions, setSort, setSortOrder] = useProductSort()
3737
const [activeFilters, setActiveFilters] = useState<IActiveFilters>([])
3838
const [moreOptions, setMoreOptions] = useState<IFilterMoreOptions>(new Map())
39-
const queryFilters: IProductFieldFilterInput = useMemo(
39+
const queryFilters: IProductFieldFilterInput[] = useMemo(
4040
() => getProductFilters(activeFilters),
4141
[activeFilters]
4242
)
@@ -111,15 +111,18 @@ export function useProducts(
111111
aggregation: filter.field,
112112
localizedCatalog: String(localizedCatalogId),
113113
}
114+
const categoryIdFilter = queryFilters.find(
115+
(value) => Object.keys(value)[0] === 'category__id'
116+
)
114117
if (search) {
115118
variables.search = search
116119
}
117120
if (currentCategoryId) {
118121
variables.currentCategoryId = currentCategoryId
119122
}
120-
if (queryFilters.category__id) {
123+
if (categoryIdFilter) {
121124
variables.currentCategoryId = (
122-
queryFilters.category__id as ICategoryTypeDefaultFilterInputType
125+
categoryIdFilter.category_id as ICategoryTypeDefaultFilterInputType
123126
).eq
124127
}
125128
graphqlApi<IGraphqlViewMoreProductFacetOptions>(

front/example-app/src/services/product.ts

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,30 @@ import {
88
} from '@elastic-suite/gally-admin-shared'
99

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

13+
type DurationKey = 'Y' | 'M' | 'W' | 'D' | 'H' | 'm' | 'S'
14+
15+
const durationFormat: Record<DurationKey, string> = {
16+
Y: 'years',
17+
M: 'months',
18+
W: 'weeks',
19+
D: 'days',
20+
H: 'hours',
21+
m: 'minutes',
22+
S: 'seconds',
23+
}
24+
25+
/* eslint-disable no-underscore-dangle */
1226
export function getProductFilters(
1327
activeFilters: IActiveFilters
14-
): IProductFieldFilterInput {
15-
return activeFilters.reduce<IProductFieldFilterInput>((acc, activeFilter) => {
28+
): IProductFieldFilterInput[] {
29+
const data: IProductFieldFilterInput[] = []
30+
activeFilters.forEach((activeFilter) => {
31+
const precAcc = data.find(
32+
(value) => Object.keys(value)[0] === activeFilter.filter.field
33+
)
34+
const acc: IProductFieldFilterInput = precAcc || {}
1635
if (activeFilter.filter.type === AggregationType.CATEGORY) {
1736
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
1837
eq: activeFilter.value,
@@ -36,7 +55,67 @@ export function getProductFilters(
3655
activeFilter.filter.field as keyof IProductFieldFilterInput
3756
] as ISelectTypeDefaultFilterInputType
3857
).in.push(activeFilter.value)
58+
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM_DATE) {
59+
if (!acc.boolFilter?._should) {
60+
acc.boolFilter = { _should: [] }
61+
}
62+
const field = activeFilter.filter.field as keyof IProductFieldFilterInput
63+
const date = parse(
64+
activeFilter.value,
65+
activeFilter.filter.date_format,
66+
new Date()
67+
)
68+
// TODO: use default_date_range_interval in graphql endpoint
69+
const incrementString = activeFilter.filter.default_date_range_interval || '1Y'
70+
const incrementNumber = Number(
71+
incrementString.substring(0, incrementString.length - 1)
72+
)
73+
const incrementType = incrementString.substring(
74+
incrementString.length - 1
75+
) as DurationKey
76+
77+
const gt = format(date, activeFilter.filter.date_format)
78+
const lt = format(
79+
add(date, {
80+
[durationFormat[incrementType]]: incrementNumber,
81+
}),
82+
activeFilter.filter.date_format
83+
)
84+
85+
acc.boolFilter._should.push({
86+
[field]: {
87+
gt,
88+
lt,
89+
},
90+
})
91+
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM) {
92+
if (!acc.boolFilter?._should) {
93+
acc.boolFilter = { _should: [] }
94+
}
95+
const field = activeFilter.filter.field as keyof IProductFieldFilterInput
96+
const arrayValue = activeFilter.value.split('-')
97+
const [firstValue, secondValue] = arrayValue
98+
99+
if (arrayValue.length > 1) {
100+
const isFirstValueIsAsterisk = firstValue === '*'
101+
const isSecondValueIsAsterisk = secondValue === '*'
102+
let data: Record<string, number> = {}
103+
104+
if (isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
105+
data = { lt: Number(secondValue) }
106+
} else if (!isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
107+
data = { gte: Number(firstValue), lte: Number(secondValue) }
108+
} else {
109+
data = { gt: Number(firstValue) }
110+
}
111+
112+
acc.boolFilter._should.push({
113+
[field]: data,
114+
})
115+
}
39116
}
40-
return acc
41-
}, {})
42-
}
117+
if (!precAcc) data.push(acc)
118+
})
119+
120+
return data
121+
}

0 commit comments

Comments
 (0)