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: Filter dependencies are not being applied in default values #19698

Merged
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 @@ -73,7 +73,10 @@ import { waitForAsyncData } from 'src/middleware/asyncEvent';
import { cacheWrapper } from 'src/utils/cacheWrapper';
import { ClientErrorObject } from 'src/utils/getClientErrorObject';
import { SingleValueType } from 'src/filters/components/Range/SingleValueType';
import { getFormData } from 'src/dashboard/components/nativeFilters/utils';
import {
getFormData,
mergeExtraFormData,
} from 'src/dashboard/components/nativeFilters/utils';
import {
ALLOW_DEPENDENCIES as TYPES_SUPPORT_DEPENDENCIES,
getFiltersConfigModalTestId,
Expand Down Expand Up @@ -346,10 +349,11 @@ const FiltersConfigForm = (
const forceUpdate = useForceUpdate();
const [datasetDetails, setDatasetDetails] = useState<Record<string, any>>();
const defaultFormFilter = useMemo(() => ({}), []);
const formValues = form.getFieldValue('filters')?.[filterId];
const filters = form.getFieldValue('filters');
const formValues = filters?.[filterId];
const formFilter = formValues || undoFormValues || defaultFormFilter;

const dependencies =
const dependencies: string[] =
formFilter?.dependencies || filterToEdit?.cascadeParentIds;

const nativeFilterItems = getChartMetadataRegistry().items;
Expand Down Expand Up @@ -439,6 +443,21 @@ const FiltersConfigForm = (
forceUpdate();
};

// Calculates the dependencies default values to be used
// to extract the available values to the filter
let dependenciesDefaultValues = {};
if (dependencies && dependencies.length > 0 && filters) {
dependencies.forEach(dependency => {
const extraFormData = filters[dependency]?.defaultDataMask?.extraFormData;
dependenciesDefaultValues = mergeExtraFormData(
dependenciesDefaultValues,
extraFormData,
);
});
}

const dependenciesText = JSON.stringify(dependenciesDefaultValues);

const refreshHandler = useCallback(
(force = false) => {
if (!hasDataset || !formFilter?.dataset?.value) {
Expand All @@ -450,6 +469,9 @@ const FiltersConfigForm = (
groupby: formFilter?.column,
...formFilter,
});

formData.extra_form_data = dependenciesDefaultValues;

setNativeFilterFieldValuesWrapper({
defaultValueQueriesData: null,
isDataDirty: false,
Expand Down Expand Up @@ -499,14 +521,19 @@ const FiltersConfigForm = (
});
});
},
[filterId, forceUpdate, form, formFilter, hasDataset],
[filterId, forceUpdate, form, formFilter, hasDataset, dependenciesText],
);

// TODO: refreshHandler changes itself because of the dependencies. Needs refactor.
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => refreshHandler(), [dependenciesText]);

const newFormData = getFormData({
datasetId,
groupby: hasColumn ? formFilter?.column : undefined,
...formFilter,
});
newFormData.extra_form_data = dependenciesDefaultValues;

const [hasDefaultValue, isRequired, defaultValueTooltip, setHasDefaultValue] =
useDefaultValue(formFilter, filterToEdit);
Expand Down Expand Up @@ -1084,6 +1111,11 @@ const FiltersConfigForm = (
tooltip={defaultValueTooltip}
onChange={value => {
setHasDefaultValue(value);
if (!value) {
setNativeFilterFieldValues(form, filterId, {
defaultDataMask: null,
});
}
formChanged();
}}
>
Expand Down