Skip to content

Commit

Permalink
fix(native-filters): Fix update ownState (#17181)
Browse files Browse the repository at this point in the history
* fix:fix get permission function

* fix: fix own state update

* refactor: fix CR notes
  • Loading branch information
simcha90 authored Oct 26, 2021
1 parent ef01cbb commit cf284ba
Showing 1 changed file with 57 additions and 24 deletions.
81 changes: 57 additions & 24 deletions superset-frontend/src/dashboard/components/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,55 @@ class Dashboard extends React.PureComponent {
return Object.values(this.props.charts);
}

isFilterKeyRemoved(filterKey) {
const { appliedFilters } = this;
const { activeFilters } = this.props;

// refresh charts if a filter was removed, added, or changed
const currFilterKeys = Object.keys(activeFilters);
const appliedFilterKeys = Object.keys(appliedFilters);

return (
!currFilterKeys.includes(filterKey) &&
appliedFilterKeys.includes(filterKey)
);
}

isFilterKeyNewlyAdded(filterKey) {
const { appliedFilters } = this;
const appliedFilterKeys = Object.keys(appliedFilters);

return !appliedFilterKeys.includes(filterKey);
}

isFilterKeyChangedValue(filterKey) {
const { appliedFilters } = this;
const { activeFilters } = this.props;

return !areObjectsEqual(
appliedFilters[filterKey].values,
activeFilters[filterKey].values,
{
ignoreUndefined: true,
},
);
}

isFilterKeyChangedScope(filterKey) {
const { appliedFilters } = this;
const { activeFilters } = this.props;

return !areObjectsEqual(
appliedFilters[filterKey].scope,
activeFilters[filterKey].scope,
);
}

hasFilterKeyValues(filterKey) {
const { appliedFilters } = this;
return Object.keys(appliedFilters[filterKey]?.values ?? []).length;
}

applyFilters() {
const { appliedFilters } = this;
const { activeFilters, ownDataCharts } = this.props;
Expand All @@ -235,37 +284,21 @@ class Dashboard extends React.PureComponent {
);
[...allKeys].forEach(filterKey => {
if (
!currFilterKeys.includes(filterKey) &&
appliedFilterKeys.includes(filterKey)
this.isFilterKeyRemoved(filterKey) ||
this.isFilterKeyNewlyAdded(filterKey)
) {
// filterKey is removed?
affectedChartIds.push(...appliedFilters[filterKey].scope);
} else if (!appliedFilterKeys.includes(filterKey)) {
// filterKey is newly added?
affectedChartIds.push(...activeFilters[filterKey].scope);
// check if there are values in filter, if no, there is was added only ownState, so no need reload other charts
if (this.hasFilterKeyValues(filterKey)) {
affectedChartIds.push(...appliedFilters[filterKey].scope);
}
} else {
// if filterKey changes value,
// update charts in its scope
if (
!areObjectsEqual(
appliedFilters[filterKey].values,
activeFilters[filterKey].values,
{
ignoreUndefined: true,
},
)
) {
if (this.isFilterKeyChangedValue(filterKey)) {
affectedChartIds.push(...activeFilters[filterKey].scope);
}

// if filterKey changes scope,
// update all charts in its scope
if (
!areObjectsEqual(
appliedFilters[filterKey].scope,
activeFilters[filterKey].scope,
)
) {
if (this.isFilterKeyChangedScope(filterKey)) {
const chartsInScope = (activeFilters[filterKey].scope || []).concat(
appliedFilters[filterKey].scope || [],
);
Expand Down

0 comments on commit cf284ba

Please sign in to comment.