/
taxonomicPropertyFilterLogic.ts
73 lines (66 loc) 路 2.75 KB
/
taxonomicPropertyFilterLogic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { kea } from 'kea'
import { propertyFilterLogic } from 'lib/components/PropertyFilters/propertyFilterLogic'
import { TaxonomicPropertyFilterLogicProps } from 'lib/components/PropertyFilters/types'
import { AnyPropertyFilter, PropertyFilterValue, PropertyOperator } from '~/types'
import { taxonomicPropertyFilterLogicType } from './taxonomicPropertyFilterLogicType'
import { cohortsModel } from '~/models/cohortsModel'
export const taxonomicPropertyFilterLogic = kea<taxonomicPropertyFilterLogicType>({
path: (key) => ['lib', 'components', 'PropertyFilters', 'components', 'taxonomicPropertyFilterLogic', key],
props: {} as TaxonomicPropertyFilterLogicProps,
key: (props) => `${props.pageKey}-${props.filterIndex}`,
connect: (props: TaxonomicPropertyFilterLogicProps) => ({
values: [propertyFilterLogic(props), ['filters']],
}),
actions: {
selectItem: (propertyType?: string, propertyKey?: PropertyFilterValue) => ({ propertyType, propertyKey }),
openDropdown: true,
closeDropdown: true,
},
reducers: {
dropdownOpen: [
false,
{
openDropdown: () => true,
closeDropdown: () => false,
},
],
},
selectors: {
filter: [
(s) => [s.filters, (_, props) => props.filterIndex],
(filters, filterIndex): AnyPropertyFilter | null => filters[filterIndex] || null,
],
selectedCohortName: [
(s) => [s.filter, cohortsModel.selectors.cohorts],
(filter, cohorts) => (filter?.type === 'cohort' ? cohorts.find((c) => c.id === filter?.value)?.name : null),
],
},
listeners: ({ actions, values, props }) => ({
selectItem: ({ propertyType, propertyKey }) => {
if (propertyKey && propertyType) {
if (propertyType === 'cohort') {
propertyFilterLogic(props).actions.setFilter(
props.filterIndex,
'id',
propertyKey,
null,
propertyType
)
} else {
const operator =
propertyKey === '$active_feature_flags'
? PropertyOperator.IContains
: values.filter?.operator || PropertyOperator.Exact
propertyFilterLogic(props).actions.setFilter(
props.filterIndex,
propertyKey.toString(),
null, // Reset value field
operator,
propertyType
)
}
actions.closeDropdown()
}
},
}),
})