Skip to content

Commit

Permalink
fix: Preserve filters from previous drillings in drill by (#23831)
Browse files Browse the repository at this point in the history
  • Loading branch information
kgabryje authored Apr 26, 2023
1 parent 109f51b commit 5f03549
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions superset-frontend/src/components/Chart/DrillBy/DrillByModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ export default function DrillByModal({

const {
column: currentColumn,
filters,
groupbyFieldName = drillByConfig.groupbyFieldName,
adhocFilterFieldName = drillByConfig.adhocFilterFieldName ||
DEFAULT_ADHOC_FILTER_FIELD_NAME,
} = drillByConfigs[drillByConfigs.length - 1] || {};

const initialGroupbyColumns = useMemo(
Expand All @@ -176,7 +173,7 @@ export default function DrillByModal({
[...initialGroupbyColumns, column].filter(isDefined),
);
const [breadcrumbsData, setBreadcrumbsData] = useState<DrillByBreadcrumb[]>([
{ groupby: initialGroupbyColumns, filters },
{ groupby: initialGroupbyColumns, filters: drillByConfig.filters },
{ groupby: column || [] },
]);

Expand Down Expand Up @@ -220,6 +217,20 @@ export default function DrillByModal({
[getNewGroupby],
);

const getFiltersFromConfigsByFieldName = useCallback(
() =>
drillByConfigs.reduce((acc, config) => {
const adhocFilterFieldName =
config.adhocFilterFieldName || DEFAULT_ADHOC_FILTER_FIELD_NAME;
acc[adhocFilterFieldName] = [
...(acc[adhocFilterFieldName] || []),
...config.filters.map(filter => simpleFilterToAdhoc(filter)),
];
return acc;
}, {}),
[drillByConfigs],
);

const onBreadcrumbClick = useCallback(
(breadcrumb: DrillByBreadcrumb, index: number) => {
setDrillByConfigs(prevConfigs => prevConfigs.slice(0, index));
Expand Down Expand Up @@ -263,16 +274,16 @@ export default function DrillByModal({
updatedFormData[groupbyFieldName] = getNewGroupby(currentColumn);
}

if (adhocFilterFieldName && Array.isArray(filters)) {
const adhocFilters = filters.map(filter => simpleFilterToAdhoc(filter));
const adhocFilters = getFiltersFromConfigsByFieldName();
Object.keys(adhocFilters).forEach(adhocFilterFieldName => {
updatedFormData = {
...updatedFormData,
[adhocFilterFieldName]: [
...ensureIsArray(formData[adhocFilterFieldName]),
...adhocFilters,
...adhocFilters[adhocFilterFieldName],
],
};
}
});

updatedFormData.slice_id = 0;
delete updatedFormData.slice_name;
Expand All @@ -281,11 +292,10 @@ export default function DrillByModal({
}, [
currentFormData,
currentColumn,
filters,
adhocFilterFieldName,
formData,
groupbyFieldName,
getFiltersFromConfigsByFieldName,
getNewGroupby,
formData,
]);

useEffect(() => {
Expand Down

0 comments on commit 5f03549

Please sign in to comment.