Skip to content

Commit 32652d5

Browse files
committed
Unify function to set segment filter
1 parent 1adcfd6 commit 32652d5

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

assets/js/dashboard/filtering/segments.test.ts

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { remapToApiFilters } from '../util/filters'
22
import {
33
formatSegmentIdAsLabelKey,
4-
getSearchToApplySingleSegmentFilter,
4+
getSearchToSetSegmentFilter,
55
getSegmentNamePlaceholder,
66
isSegmentIdLabelKey,
77
parseApiSegmentData,
@@ -64,9 +64,57 @@ describe(`${parseApiSegmentData.name}`, () => {
6464
})
6565
})
6666

67-
describe(`${getSearchToApplySingleSegmentFilter.name}`, () => {
68-
test('generated search function applies single segment correctly', () => {
69-
const searchFunction = getSearchToApplySingleSegmentFilter({
67+
describe(`${getSearchToSetSegmentFilter.name}`, () => {
68+
test('generated search function omits other filters segment correctly', () => {
69+
const searchFunction = getSearchToSetSegmentFilter(
70+
{
71+
name: 'APAC',
72+
id: 500
73+
},
74+
{ omitAllOtherFilters: true }
75+
)
76+
const existingSearch = {
77+
date: '2025-02-10',
78+
filters: [
79+
['is', 'country', ['US']],
80+
['is', 'page', ['/blog']]
81+
],
82+
labels: { US: 'United States' }
83+
}
84+
expect(searchFunction(existingSearch)).toEqual({
85+
date: '2025-02-10',
86+
filters: [['is', 'segment', [500]]],
87+
labels: { 'segment-500': 'APAC' }
88+
})
89+
})
90+
91+
test('generated search function replaces existing segment filter correctly', () => {
92+
const searchFunction = getSearchToSetSegmentFilter({
93+
name: 'APAC',
94+
id: 500
95+
})
96+
const existingSearch = {
97+
date: '2025-02-10',
98+
filters: [
99+
['is', 'segment', [100]],
100+
['is', 'country', ['US']],
101+
['is', 'page', ['/blog']]
102+
],
103+
labels: { US: 'United States', 'segment-100': 'Scandinavia' }
104+
}
105+
expect(searchFunction(existingSearch)).toEqual({
106+
date: '2025-02-10',
107+
filters: [
108+
['is', 'segment', [500]],
109+
['is', 'country', ['US']],
110+
['is', 'page', ['/blog']]
111+
],
112+
labels: { US: 'United States', 'segment-500': 'APAC' }
113+
})
114+
})
115+
116+
test('generated search function sets new segment filter correctly', () => {
117+
const searchFunction = getSearchToSetSegmentFilter({
70118
name: 'APAC',
71119
id: 500
72120
})
@@ -80,8 +128,12 @@ describe(`${getSearchToApplySingleSegmentFilter.name}`, () => {
80128
}
81129
expect(searchFunction(existingSearch)).toEqual({
82130
date: '2025-02-10',
83-
filters: [['is', 'segment', [500]]],
84-
labels: { 'segment-500': 'APAC' }
131+
filters: [
132+
['is', 'segment', [500]],
133+
['is', 'country', ['US']],
134+
['is', 'page', ['/blog']]
135+
],
136+
labels: { US: 'United States', 'segment-500': 'APAC' }
85137
})
86138
})
87139
})

assets/js/dashboard/filtering/segments.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,22 @@ export const parseApiSegmentData = ({
105105
...rest
106106
})
107107

108-
export function getSearchToApplySingleSegmentFilter(
109-
segment: Pick<SavedSegment, 'id' | 'name'>
108+
export function getSearchToSetSegmentFilter(
109+
segment: Pick<SavedSegment, 'id' | 'name'>,
110+
options: { omitAllOtherFilters?: boolean } = {}
110111
): Required<AppNavigationTarget>['search'] {
111112
return (search) => {
112-
const filters = [['is', 'segment', [segment.id]]]
113-
const labels = cleanLabels(filters, {}, 'segment', {
113+
const otherFilters = (
114+
(Array.isArray(search.filters) ? search.filters : []) as Filter[]
115+
).filter((f) => !isSegmentFilter(f))
116+
const currentLabels = search.labels ?? {}
117+
118+
const filters = [
119+
['is', 'segment', [segment.id]],
120+
...(options.omitAllOtherFilters ? [] : otherFilters)
121+
]
122+
123+
const labels = cleanLabels(filters, currentLabels, 'segment', {
114124
[formatSegmentIdAsLabelKey(segment.id)]: segment.name
115125
})
116126
return {

assets/js/dashboard/segments/routeless-segment-modals.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
UpdateSegmentModal
66
} from './segment-modals'
77
import {
8-
getSearchToApplySingleSegmentFilter,
8+
getSearchToSetSegmentFilter,
99
getSegmentNamePlaceholder,
1010
handleSegmentResponse,
1111
SavedSegment,
@@ -67,7 +67,9 @@ export const RoutelessSegmentModals = () => {
6767
updateOne(segment)
6868
queryClient.invalidateQueries({ queryKey: ['segments'] })
6969
navigate({
70-
search: getSearchToApplySingleSegmentFilter(segment),
70+
search: getSearchToSetSegmentFilter(segment, {
71+
omitAllOtherFilters: true
72+
}),
7173
state: {
7274
expandedSegment: null
7375
}
@@ -104,7 +106,9 @@ export const RoutelessSegmentModals = () => {
104106
addOne(segment)
105107
queryClient.invalidateQueries({ queryKey: ['segments'] })
106108
navigate({
107-
search: getSearchToApplySingleSegmentFilter(segment),
109+
search: getSearchToSetSegmentFilter(segment, {
110+
omitAllOtherFilters: true
111+
}),
108112
state: {
109113
expandedSegment: null
110114
}

0 commit comments

Comments
 (0)