Skip to content

Commit

Permalink
fix: Saving Mixed Chart with dashboard filter applied breaks adhoc_fi…
Browse files Browse the repository at this point in the history
…lter_b (#25877)
  • Loading branch information
kgabryje authored and pull[bot] committed Nov 9, 2024
1 parent d0c3b14 commit a738225
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
19 changes: 13 additions & 6 deletions superset-frontend/src/explore/actions/saveModalActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function saveSliceSuccess(data) {
return { type: SAVE_SLICE_SUCCESS, data };
}

const extractAddHocFiltersFromFormData = formDataToHandle =>
const extractAdhocFiltersFromFormData = formDataToHandle =>
Object.entries(formDataToHandle).reduce(
(acc, [key, value]) =>
ADHOC_FILTER_REGEX.test(key)
Expand All @@ -71,7 +71,7 @@ export const getSlicePayload = (
owners,
formDataFromSlice = {},
) => {
const adhocFilters = extractAddHocFiltersFromFormData(
const adhocFilters = extractAdhocFiltersFromFormData(
formDataWithNativeFilters,
);

Expand All @@ -80,10 +80,17 @@ export const getSlicePayload = (
// to filter the chart. Before, any time range filter applied in the dashboard
// would end up as an extra filter and when overwriting the chart the original
// time range adhoc_filter was lost
if (isEmpty(adhocFilters?.adhoc_filters) && !isEmpty(formDataFromSlice)) {
formDataFromSlice?.adhoc_filters?.forEach(filter => {
if (filter.operator === Operators.TEMPORAL_RANGE && !filter.isExtra) {
adhocFilters.adhoc_filters.push({ ...filter, comparator: 'No filter' });
if (!isEmpty(formDataFromSlice)) {
Object.keys(adhocFilters || {}).forEach(adhocFilterKey => {
if (isEmpty(adhocFilters[adhocFilterKey])) {
formDataFromSlice?.[adhocFilterKey]?.forEach(filter => {
if (filter.operator === Operators.TEMPORAL_RANGE && !filter.isExtra) {
adhocFilters[adhocFilterKey].push({
...filter,
comparator: 'No filter',
});
}
});
}
});
}
Expand Down
53 changes: 53 additions & 0 deletions superset-frontend/src/explore/actions/saveModalActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,57 @@ describe('getSlicePayload', () => {
formDataFromSlice.adhoc_filters,
);
});

test('should return the correct payload when formDataWithNativeFilters has a filter with isExtra set to true in mixed chart', () => {
const formDataFromSliceWithAdhocFilterB = {
...formDataFromSlice,
adhoc_filters_b: [
{
clause: 'WHERE',
subject: 'year',
operator: 'TEMPORAL_RANGE',
comparator: 'No filter',
expressionType: 'SIMPLE',
},
],
};
const formDataWithAdhocFiltersWithExtra = {
...formDataWithNativeFilters,
viz_type: 'mixed_timeseries',
adhoc_filters: [
{
clause: 'WHERE',
subject: 'year',
operator: 'TEMPORAL_RANGE',
comparator: 'No filter',
expressionType: 'SIMPLE',
isExtra: true,
},
],
adhoc_filters_b: [
{
clause: 'WHERE',
subject: 'year',
operator: 'TEMPORAL_RANGE',
comparator: 'No filter',
expressionType: 'SIMPLE',
isExtra: true,
},
],
};
const result = getSlicePayload(
sliceName,
formDataWithAdhocFiltersWithExtra,
dashboards,
owners,
formDataFromSliceWithAdhocFilterB,
);

expect(JSON.parse(result.params).adhoc_filters).toEqual(
formDataFromSliceWithAdhocFilterB.adhoc_filters,
);
expect(JSON.parse(result.params).adhoc_filters_b).toEqual(
formDataFromSliceWithAdhocFilterB.adhoc_filters_b,
);
});
});

0 comments on commit a738225

Please sign in to comment.