Skip to content

Commit

Permalink
break out label logic
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro committed Aug 13, 2021
1 parent 388ac55 commit ef33f65
Showing 1 changed file with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { DataMaskStateWithId, DataMaskType } from 'src/dataMask/types';
import {
ensureIsArray,
FeatureFlag,
FilterState,
isFeatureEnabled,
} from '@superset-ui/core';
import { Layout } from '../../types';
Expand Down Expand Up @@ -54,6 +55,16 @@ type Filter = {
datasourceId: string;
};

const extractLabel = (filter?: FilterState): string | null => {
if (filter?.label) {
return filter.label;
}
if (filter?.value) {
return ensureIsArray(filter?.value).join(', ');
}
return null;
};

const selectIndicatorValue = (
columnKey: string,
filter: Filter,
Expand Down Expand Up @@ -186,16 +197,16 @@ export const selectNativeIndicatorsForChart = (
const rejectedColumns = getRejectedColumns(chart);

const getStatus = ({
value,
label,
column,
type = DataMaskType.NativeFilters,
}: {
value: any;
label: string | null;
column?: string;
type?: DataMaskType;
}): IndicatorStatus => {
// a filter is only considered unset if it's value is null
const hasValue = value !== null;
const hasValue = label !== null;
if (type === DataMaskType.CrossFilters && hasValue) {
return IndicatorStatus.CrossFilterApplied;
}
Expand Down Expand Up @@ -224,19 +235,14 @@ export const selectNativeIndicatorsForChart = (
)
.map(nativeFilter => {
const column = nativeFilter.targets[0]?.column?.name;
let value =
dataMask[nativeFilter.id]?.filterState?.label ??
dataMask[nativeFilter.id]?.filterState?.value ??
null;
if (!Array.isArray(value) && value !== null) {
value = [value];
}
const filterState = dataMask[nativeFilter.id]?.filterState;
const label = extractLabel(filterState);
return {
column,
name: nativeFilter.name,
path: [nativeFilter.id],
status: getStatus({ value, column }),
value,
status: getStatus({ label, column }),
value: label,
};
});
}
Expand All @@ -253,12 +259,9 @@ export const selectNativeIndicatorsForChart = (
),
)
.map(chartConfig => {
const value = ensureIsArray(
dataMask[chartConfig.id]?.filterState?.label ??
dataMask[chartConfig.id]?.filterState?.value ??
null,
);
const filtersState = dataMask[chartConfig.id]?.filterState?.filters;
const filterState = dataMask[chartConfig.id]?.filterState;
const label = extractLabel(filterState);
const filtersState = filterState?.filters;
const column = filtersState && Object.keys(filtersState)[0];

const dashboardLayoutItem = Object.values(dashboardLayout).find(
Expand All @@ -272,10 +275,10 @@ export const selectNativeIndicatorsForChart = (
dashboardLayoutItem?.id,
],
status: getStatus({
value,
label,
type: DataMaskType.CrossFilters,
}),
value,
value: label,
};
})
.filter(filter => filter.status === IndicatorStatus.CrossFilterApplied);
Expand Down

0 comments on commit ef33f65

Please sign in to comment.