Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Updates the selected values when changing the native filter type, column or default value #16833

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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