/
intervalFilterLogic.ts
59 lines (54 loc) 路 1.97 KB
/
intervalFilterLogic.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
import { kea } from 'kea'
import { router } from 'kea-router'
import { objectsEqual } from 'lib/utils'
import { intervalFilterLogicType } from './intervalFilterLogicType'
import { IntervalKeyType } from 'lib/components/IntervalFilter/intervals'
export const intervalFilterLogic = kea<intervalFilterLogicType>({
path: ['lib', 'components', 'IntervalFilter', 'intervalFilterLogic'],
actions: () => ({
setIntervalFilter: (filter: IntervalKeyType) => ({ filter }),
setDateFrom: (dateFrom: string) => ({ dateFrom }),
}),
reducers: {
interval: [
null as null | IntervalKeyType,
{
setIntervalFilter: (_, { filter }) => filter,
},
],
dateFrom: [
null as null | string,
{
setDateFrom: (_, { dateFrom }) => dateFrom,
},
],
},
listeners: ({ values }) => ({
setIntervalFilter: () => {
const { interval, ...searchParams } = router.values.searchParams
const { pathname } = router.values.location
searchParams.interval = values.interval
if (!objectsEqual(interval, values.interval)) {
router.actions.replace(pathname, searchParams, router.values.hashParams)
}
},
setDateFrom: () => {
const { date_from, ...searchParams } = router.values.searchParams
const { pathname } = router.values.location
searchParams.date_from = values.dateFrom
if (!objectsEqual(date_from, values.dateFrom)) {
router.actions.replace(pathname, searchParams, router.values.hashParams)
}
},
}),
urlToAction: ({ actions }) => ({
'/insights': (_, { interval, date_from }) => {
if (interval) {
actions.setIntervalFilter(interval)
}
if (date_from) {
actions.setDateFrom(date_from)
}
},
}),
})