Skip to content

Commit

Permalink
fix: Updates the selected values when changing the native filter type…
Browse files Browse the repository at this point in the history
…, column or default value (apache#16833)

(cherry picked from commit 1ff682f)
  • Loading branch information
michael-s-molina authored and Steven Uray committed Sep 27, 2021
1 parent 233a52c commit b27631d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags';
import { updateDataMask, clearDataMask } from 'src/dataMask/actions';
import { DataMaskStateWithId, DataMaskWithId } from 'src/dataMask/types';
import { useImmer } from 'use-immer';
import { isEmpty, isEqual } from 'lodash';
import { testWithId } from 'src/utils/testUtils';
import { Filter } from 'src/dashboard/components/nativeFilters/types';
import Loading from 'src/components/Loading';
Expand Down Expand Up @@ -162,28 +163,6 @@ const FilterBar: React.FC<FiltersBarProps> = ({
const filterValues = Object.values<Filter>(filters);
const [isFilterSetChanged, setIsFilterSetChanged] = useState(false);

useEffect(() => {
setDataMaskSelected(() => dataMaskApplied);
}, [JSON.stringify(dataMaskApplied), setDataMaskSelected]);

// reset filter state if filter type changes
useEffect(() => {
setDataMaskSelected(draft => {
Object.values(filters).forEach(filter => {
if (
filter.filterType !== previousFilters?.[filter.id]?.filterType &&
previousFilters?.[filter.id]?.filterType !== undefined
) {
draft[filter.id] = getInitialDataMask(filter.id) as DataMaskWithId;
}
});
});
}, [
JSON.stringify(filters),
JSON.stringify(previousFilters),
setDataMaskSelected,
]);

const handleFilterSelectionChange = (
filter: Pick<Filter, 'id'> & Partial<Filter>,
dataMask: Partial<DataMask>,
Expand Down Expand Up @@ -232,6 +211,37 @@ const FilterBar: React.FC<FiltersBarProps> = ({
[history],
);

useEffect(() => {
if (previousFilters) {
const updates = {};
Object.values(filters).forEach(currentFilter => {
const currentType = currentFilter.filterType;
const currentTargets = currentFilter.targets;
const currentDataMask = currentFilter.defaultDataMask;
const previousFilter = previousFilters?.[currentFilter.id];
const previousType = previousFilter?.filterType;
const previousTargets = previousFilter?.targets;
const previousDataMask = previousFilter?.defaultDataMask;
const typeChanged = currentType !== previousType;
const targetsChanged = !isEqual(currentTargets, previousTargets);
const dataMaskChanged = !isEqual(currentDataMask, previousDataMask);

if (typeChanged || targetsChanged || dataMaskChanged) {
updates[currentFilter.id] = getInitialDataMask(currentFilter.id);
}
});

if (!isEmpty(updates)) {
setDataMaskSelected(draft => ({ ...draft, ...updates }));
Object.keys(updates).forEach(key => dispatch(clearDataMask(key)));
}
}
}, [JSON.stringify(filters), JSON.stringify(previousFilters)]);

useEffect(() => {
setDataMaskSelected(() => dataMaskApplied);
}, [JSON.stringify(dataMaskApplied), setDataMaskSelected]);

const dataMaskAppliedText = JSON.stringify(dataMaskApplied);
useEffect(() => {
publishDataMask(dataMaskApplied);
Expand Down
9 changes: 1 addition & 8 deletions superset-frontend/src/dataMask/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,7 @@ export function updateDataMask(
}

export function clearDataMask(filterId: string | number) {
return updateDataMask(
filterId,
getInitialDataMask(filterId, {
filterState: {
value: null,
},
}),
);
return updateDataMask(filterId, getInitialDataMask(filterId));
}

export type AnyDataMaskAction =
Expand Down
4 changes: 1 addition & 3 deletions superset-frontend/src/dataMask/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ export function getInitialDataMask(
return {
...otherProps,
extraFormData: {},
filterState: {
value: undefined,
},
filterState: {},
ownState: {},
...moreProps,
} as DataMaskWithId;
Expand Down

0 comments on commit b27631d

Please sign in to comment.