Skip to content

Commit

Permalink
added onDelete prop on filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jcger committed Nov 22, 2023
1 parent 88a4e04 commit c915c16
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ export interface FilterConfig {
label: string;
isActive: boolean;
isAvailable: boolean;
onDeactivate: (params: { onChange: FilterChangeHandler }) => void;
render: (params: FilterConfigRenderParams) => React.ReactNode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export const useCustomFieldsFilterConfig = () => {
isActive: false,
isAvailable: type === CustomFieldTypes.TOGGLE,
label: buttonLabel,
onDeactivate: ({ onChange }) => {
onChange({
filterId: fieldKey,
selectedOptionKeys: [],
customFieldType: type,
});
},
render: ({ filterOptions, onChange }: FilterConfigRenderParams) => {
const onCustomFieldChange = ({
filterId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { useState, useEffect } from 'react';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import { LOCAL_STORAGE_KEYS } from '../../../../common/constants';
import type { FilterConfig, FilterConfigState } from './types';
import type { FilterChangeHandler, FilterConfig, FilterConfigState } from './types';
import { useCustomFieldsFilterConfig } from './use_custom_fields_filter_config';
import { MoreFiltersSelectable } from './more_filters_selectable';
import { useCasesContext } from '../../cases_context/use_cases_context';
Expand All @@ -31,7 +31,13 @@ const deserializeFilterVisibilityMap = (value: string): Map<string, FilterConfig
);
};

export const useFilterConfig = ({ systemFilterConfig }: { systemFilterConfig: FilterConfig[] }) => {
export const useFilterConfig = ({
systemFilterConfig,
onFilterOptionChange,
}: {
systemFilterConfig: FilterConfig[];
onFilterOptionChange: FilterChangeHandler;
}) => {
const { appId } = useCasesContext();
const { customFieldsFilterConfig } = useCustomFieldsFilterConfig();
const [filters, setFilters] = useState<Map<string, FilterConfig>>(
Expand Down Expand Up @@ -64,6 +70,10 @@ export const useFilterConfig = ({ systemFilterConfig }: { systemFilterConfig: Fi
newFilterVisibilityMap.set(key, { key, isActive: true });
}
} else {
const filter = filters.get(key);
if (filter) {
filter.onDeactivate({ onChange: onFilterOptionChange });
}
newFilterVisibilityMap.set(key, { key, isActive: false });
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { SeverityFilter } from '../severity_filter';
import { AssigneesFilterPopover } from '../assignees_filter';
import type { CurrentUserProfile } from '../../types';
import type { AssigneesFilteringSelection } from '../../user_profiles/types';
import type { FilterConfig, FilterConfigRenderParams } from './types';
import type { FilterChangeHandler, FilterConfig, FilterConfigRenderParams } from './types';

interface UseFilterConfigProps {
availableSolutions: string[];
Expand Down Expand Up @@ -56,6 +56,9 @@ export const getSystemFilterConfig = ({
label: i18n.SEVERITY,
isActive: true,
isAvailable: true,
onDeactivate: ({ onChange }: { onChange: FilterChangeHandler }) => {
onChange({ filterId: 'severity', selectedOptionKeys: [] });
},
render: ({ filterOptions, onChange }: FilterConfigRenderParams) => (
<SeverityFilter selectedOptionKeys={filterOptions.severity} onChange={onChange} />
),
Expand All @@ -65,6 +68,9 @@ export const getSystemFilterConfig = ({
label: i18n.STATUS,
isActive: true,
isAvailable: true,
onDeactivate: ({ onChange }: { onChange: FilterChangeHandler }) => {
onChange({ filterId: 'status', selectedOptionKeys: [] });
},
render: ({ filterOptions, onChange }: FilterConfigRenderParams) => (
<StatusFilter
selectedOptionKeys={filterOptions?.status}
Expand All @@ -81,6 +87,9 @@ export const getSystemFilterConfig = ({
label: i18n.ASSIGNEES,
isActive: true,
isAvailable: caseAssignmentAuthorized && !isSelectorView,
onDeactivate: ({ onChange }: { onChange: FilterChangeHandler }) => {
onChange({ filterId: 'assignee', selectedOptionKeys: [] });
},
render: ({ filterOptions, onChange }: FilterConfigRenderParams) => {
return (
<AssigneesFilterPopover
Expand All @@ -97,6 +106,9 @@ export const getSystemFilterConfig = ({
label: i18n.TAGS,
isActive: true,
isAvailable: true,
onDeactivate: ({ onChange }: { onChange: FilterChangeHandler }) => {
onChange({ filterId: 'tags', selectedOptionKeys: [] });
},
render: ({ filterOptions, onChange }: FilterConfigRenderParams) => (
<MultiSelectFilter
buttonLabel={i18n.TAGS}
Expand All @@ -114,6 +126,9 @@ export const getSystemFilterConfig = ({
label: i18n.CATEGORIES,
isActive: true,
isAvailable: true,
onDeactivate: ({ onChange }: { onChange: FilterChangeHandler }) => {
onChange({ filterId: 'category', selectedOptionKeys: [] });
},
render: ({ filterOptions, onChange }: FilterConfigRenderParams) => (
<MultiSelectFilter
buttonLabel={i18n.CATEGORIES}
Expand All @@ -131,6 +146,11 @@ export const getSystemFilterConfig = ({
label: i18n.SOLUTION,
isActive: true,
isAvailable: availableSolutions.length > 1,
onDeactivate: ({ onChange }: { onChange: FilterChangeHandler }) => {
if (availableSolutions.length > 1) {
onChange({ filterId: 'owner', selectedOptionKeys: [] });
}
},
render: ({ filterOptions, onChange }: FilterConfigRenderParams) => (
<SolutionFilter
onChange={onChange}
Expand Down
48 changes: 24 additions & 24 deletions x-pack/plugins/cases/public/components/all_cases/table_filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,6 @@ const CasesTableFiltersComponent = ({
[selectedAssignees, onFilterChanged]
);

const { systemFilterConfig } = useSystemFilterConfig({
availableSolutions,
caseAssignmentAuthorized,
categories,
countClosedCases,
countInProgressCases,
countOpenCases,
currentUserProfile,
handleSelectedAssignees,
hiddenStatuses,
isLoading,
isSelectorView,
selectedAssignees,
tags,
});

const {
filters: activeFilters,
moreFiltersSelectableComponent: MoreFiltersSelectable,
selectableOptions,
activeSelectableOptionKeys,
onFilterConfigChange,
} = useFilterConfig({ systemFilterConfig });

const onFilterOptionChange: FilterChangeHandler = ({
filterId,
selectedOptionKeys,
Expand All @@ -115,6 +91,30 @@ const CasesTableFiltersComponent = ({
}
};

const { systemFilterConfig } = useSystemFilterConfig({
availableSolutions,
caseAssignmentAuthorized,
categories,
countClosedCases,
countInProgressCases,
countOpenCases,
currentUserProfile,
handleSelectedAssignees,
hiddenStatuses,
isLoading,
isSelectorView,
selectedAssignees,
tags,
});

const {
filters: activeFilters,
moreFiltersSelectableComponent: MoreFiltersSelectable,
selectableOptions,
activeSelectableOptionKeys,
onFilterConfigChange,
} = useFilterConfig({ systemFilterConfig, onFilterOptionChange });

const handleOnSearch = useCallback(
(newSearch) => {
const trimSearch = newSearch.trim();
Expand Down

0 comments on commit c915c16

Please sign in to comment.