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: Validation of out of scope filters and interaction with Clear All #24610

Merged
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 @@ -32,6 +32,7 @@ import { FilterCard } from '../../FilterCard';
import { FilterBarScrollContext } from '../Vertical';
import { FilterControlProps } from './types';
import { FilterCardPlacement } from '../../FilterCard/types';
import { useIsFilterInScope } from '../../state';

const StyledIcon = styled.div`
position: absolute;
Expand Down Expand Up @@ -232,10 +233,11 @@ const FilterControl = ({

const { name = '<undefined>' } = filter;

const isMissingRequiredValue = checkIsMissingRequiredValue(
filter,
filter.dataMask?.filterState,
);
const isFilterInScope = useIsFilterInScope();
const isMissingRequiredValue =
isFilterInScope(filter) &&
checkIsMissingRequiredValue(filter, filter.dataMask?.filterState);
const validateStatus = isMissingRequiredValue ? 'error' : undefined;
const isRequired = !!filter.controlValues?.enableEmptyFilter;

const {
Expand Down Expand Up @@ -292,6 +294,7 @@ const FilterControl = ({
setFilterActive={setIsFilterActive}
orientation={orientation}
overflow={overflow}
validateStatus={validateStatus}
/>
</InPortal>
<FilterControlContainer
Expand All @@ -310,7 +313,7 @@ const FilterControl = ({
<FormItem
label={label}
required={filter?.controlValues?.enableEmptyFilter}
validateStatus={isMissingRequiredValue ? 'error' : undefined}
validateStatus={validateStatus}
>
<OutPortal node={portalNode} />
</FormItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import { dispatchHoverAction, dispatchFocusAction } from './utils';
import { FilterControlProps } from './types';
import { getFormData } from '../../utils';
import { useFilterDependencies } from './state';
import { checkIsMissingRequiredValue } from '../utils';
import { useFilterOutlined } from '../useFilterOutlined';

const HEIGHT = 32;
Expand Down Expand Up @@ -95,6 +94,7 @@ const FilterValue: React.FC<FilterControlProps> = ({
setFilterActive,
orientation = FilterBarOrientation.VERTICAL,
overflow = false,
validateStatus,
}) => {
const { id, targets, filterType, adhoc_filters, time_range } = filter;
const metadata = getChartMetadataRegistry().get(filterType);
Expand Down Expand Up @@ -281,17 +281,12 @@ const FilterValue: React.FC<FilterControlProps> = ({
],
);

const isMissingRequiredValue = checkIsMissingRequiredValue(
filter,
filter.dataMask?.filterState,
);

const filterState = useMemo(
() => ({
...filter.dataMask?.filterState,
validateStatus: isMissingRequiredValue && 'error',
validateStatus,
}),
[filter.dataMask?.filterState, isMissingRequiredValue],
[filter.dataMask?.filterState, validateStatus],
);

const displaySettings = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ export interface FilterControlProps extends BaseFilterProps {
showOverflow?: boolean;
parentRef?: RefObject<any>;
setFilterActive?: (isActive: boolean) => void;
validateStatus?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { createFilterKey, updateFilterKey } from './keyValue';
import ActionButtons from './ActionButtons';
import Horizontal from './Horizontal';
import Vertical from './Vertical';
import { useSelectFiltersInScope } from '../state';

// FilterBar is just being hidden as it must still
// render fully due to encapsulated logics
Expand Down Expand Up @@ -141,6 +142,8 @@ const FilterBar: React.FC<FiltersBarProps> = ({
({ dashboardInfo }) => dashboardInfo.dash_edit_perm,
);

const [filtersInScope] = useSelectFiltersInScope(nativeFilterValues);

const handleFilterSelectionChange = useCallback(
(
filter: Pick<Filter, 'id'> & Partial<Filter>,
Expand Down Expand Up @@ -222,24 +225,32 @@ const FilterBar: React.FC<FiltersBarProps> = ({
}, [dataMaskSelected, dispatch]);

const handleClearAll = useCallback(() => {
const filterIds = Object.keys(dataMaskSelected);
filterIds.forEach(filterId => {
if (dataMaskSelected[filterId]) {
dispatch(clearDataMask(filterId));
const clearDataMaskIds: string[] = [];
let dispatchAllowed = false;
filtersInScope.filter(isNativeFilter).forEach(filter => {
const { id } = filter;
if (dataMaskSelected[id]) {
if (filter.controlValues?.enableEmptyFilter) {
dispatchAllowed = false;
}
clearDataMaskIds.push(id);
setDataMaskSelected(draft => {
if (draft[filterId].filterState?.value !== undefined) {
draft[filterId].filterState!.value = undefined;
if (draft[id].filterState?.value !== undefined) {
draft[id].filterState!.value = undefined;
}
});
}
});
}, [dataMaskSelected, dispatch, setDataMaskSelected]);
if (dispatchAllowed) {
clearDataMaskIds.forEach(id => dispatch(clearDataMask(id)));
}
}, [dataMaskSelected, dispatch, filtersInScope, setDataMaskSelected]);

useFilterUpdates(dataMaskSelected, setDataMaskSelected);
const isApplyDisabled = checkIsApplyDisabled(
dataMaskSelected,
dataMaskApplied,
nativeFilterValues,
filtersInScope.filter(isNativeFilter),
);
const isInitialized = useInitialization();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function useSelectChartTabParents() {
};
}

function useIsFilterInScope() {
export function useIsFilterInScope() {
const activeTabs = useActiveDashboardTabs();
const selectChartTabParents = useSelectChartTabParents();

Expand Down