Skip to content

Commit ff28689

Browse files
committed
fix: only save allowAll on static list filters from the filter form
1 parent 379678d commit ff28689

2 files changed

Lines changed: 38 additions & 30 deletions

File tree

‎packages/app/src/components/DashboardFiltersModal/__tests__/filterFormState.test.ts‎

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -640,31 +640,26 @@ describe('maxSelections', () => {
640640
});
641641

642642
describe('allowAll', () => {
643-
const allCapable = [
644-
['STATIC_LIST', byType.STATIC_LIST],
645-
['SQL_QUERY', byType.SQL_QUERY],
646-
] as const;
647-
648-
it.each(allCapable)(
649-
'round-trips allowAll on a %s filter',
650-
(_type, filter) => {
651-
const saved = toSavedFilter(formValues({ ...filter, allowAll: true }));
643+
it('round-trips allowAll on a static list filter', () => {
644+
const saved = toSavedFilter(
645+
formValues({ ...byType.STATIC_LIST, allowAll: true }),
646+
);
652647

653-
expect(saved).toMatchObject({ allowAll: true });
654-
expect(toFormValues(saved).allowAll).toBe(true);
655-
},
656-
);
648+
expect(saved).toMatchObject({ allowAll: true });
649+
expect(toFormValues(saved).allowAll).toBe(true);
650+
});
657651

658-
it.each(allCapable)(
659-
'stores nothing for allowAll on a single-select %s filter',
660-
(_type, filter) => {
661-
const saved = toSavedFilter(
662-
formValues({ ...filter, allowAll: true, isSingleSelect: true }),
663-
);
652+
it('stores nothing for allowAll on a single-select static list filter', () => {
653+
const saved = toSavedFilter(
654+
formValues({
655+
...byType.STATIC_LIST,
656+
allowAll: true,
657+
isSingleSelect: true,
658+
}),
659+
);
664660

665-
expect(saved).toMatchObject({ allowAll: undefined });
666-
},
667-
);
661+
expect(saved).toMatchObject({ allowAll: undefined });
662+
});
668663

669664
it('stores nothing for allowAll when it is off', () => {
670665
const saved = toSavedFilter(
@@ -673,4 +668,18 @@ describe('allowAll', () => {
673668

674669
expect(saved).toMatchObject({ allowAll: undefined });
675670
});
671+
672+
// SQL query filters don't expand "All" yet
673+
it('stores nothing for allowAll after switching a static list to a SQL query', () => {
674+
const saved = toSavedFilter(
675+
formValues({
676+
...byType.STATIC_LIST,
677+
allowAll: true,
678+
...byType.SQL_QUERY,
679+
}),
680+
);
681+
682+
expect(saved.type).toBe('SQL_QUERY');
683+
expect(saved).not.toHaveProperty('allowAll');
684+
});
676685
});

‎packages/app/src/components/DashboardFiltersModal/filterFormState.ts‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ export const toFormValues = (
104104

105105
// STATIC_LIST fields
106106
options: staticList?.options ?? [],
107-
108-
// STATIC_LIST and SQL_QUERY fields
109-
allowAll: staticList?.allowAll ?? sqlQuery?.allowAll ?? false,
107+
allowAll: staticList?.allowAll ?? false,
110108

111109
// PROMETHEUS_LABEL fields
112110
label: promqlLabel?.label ?? '',
@@ -130,7 +128,8 @@ export const toFormValues = (
130128
/** Normalizes the form values into the filter that gets stored. */
131129
export const toSavedFilter = (values: FilterFormValues): DashboardFilter => {
132130
// Pulled out of the spread below so no key is stored unless it's relevant
133-
const { isRequired, isGlobalRequirement, isSingleSelect, ...rest } = values;
131+
const { isRequired, isGlobalRequirement, isSingleSelect, allowAll, ...rest } =
132+
values;
134133
const selectionLimits = {
135134
...(isRequired
136135
? {
@@ -140,14 +139,13 @@ export const toSavedFilter = (values: FilterFormValues): DashboardFilter => {
140139
: {}),
141140
...(isSingleSelect ? { maxSelections: 1 } : {}),
142141
};
143-
// Single-select filters don't offer "All", and false isn't stored
144-
const allowAll = values.allowAll && !isSingleSelect ? true : undefined;
145142

146143
if (values.type === 'STATIC_LIST') {
147144
return DashboardFilterSchema.parse({
148145
...rest,
149146
options: values.options.map(option => option.trim()),
150-
allowAll,
147+
// Single-select filters don't offer "All", and false isn't stored
148+
allowAll: allowAll && !isSingleSelect ? true : undefined,
151149
isBroadcastEnabled: false,
152150
isVariableEnabled: true,
153151
variableName: getFilterVariableName(values),
@@ -172,7 +170,8 @@ export const toSavedFilter = (values: FilterFormValues): DashboardFilter => {
172170
...rest,
173171
source: values.source || undefined,
174172
sqlTemplate: values.sqlTemplate.trim(),
175-
allowAll,
173+
// No allowAll until SQL query filters expand "All": a static list's
174+
// checkbox value survives switching the type.
176175
isBroadcastEnabled: false,
177176
isVariableEnabled: true,
178177
variableName: getFilterVariableName(values),

0 commit comments

Comments
 (0)