Skip to content

Commit

Permalink
ISSUE #5257 - allow to create only new filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Amantini1997 committed Nov 26, 2024
1 parent 14c62a0 commit d396a18
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ import { CardFilterActionMenu } from '../../filterForm/filterForm.styles';
import { TicketFiltersContext } from '@components/viewer/cards/tickets/ticketFiltersContext';

export const FilterSelection = () => {
const { upsertFilter } = useContext(TicketFiltersContext);
const { upsertFilter, hasFilter } = useContext(TicketFiltersContext);
const [active, setActive] = useState(false);
const [selectedItem, setSelectedItem] = useState<TicketFilterListItemType>(null);
const { containerOrFederation } = useParams<ViewerParams>();
const tickets = TicketsHooksSelectors.selectTicketsRaw(containerOrFederation);
const usedTemplates = uniq(tickets.map((t) => t.type));
const templates = usedTemplates.map((t) => selectTemplateById(getState(), containerOrFederation, t));
const filterElements = templatesToFilters(templates);
const unusedFilters = filterElements.filter((f) => !hasFilter(f));
const showFiltersList = !selectedItem?.property;

const onOpen = () => setActive(true);
Expand All @@ -58,7 +59,7 @@ export const FilterSelection = () => {
TriggerButton={(
<Tooltip title={formatMessage({ id: 'viewer.card.tickets.addFilter', defaultMessage: 'Add Filter' })}>
<div>
<CardAction disabled={!filterElements.length} $active={active}>
<CardAction disabled={!unusedFilters.length} $active={active}>
<FennelIcon />
</CardAction>
</div>
Expand All @@ -67,7 +68,7 @@ export const FilterSelection = () => {
onOpen={onOpen}
onClose={onClose}
>
<SearchContextComponent items={filterElements}>
<SearchContextComponent items={unusedFilters}>
<DrillDownList $visibleIndex={showFiltersList ? 0 : 1}>
<DrillDownItem $visible={showFiltersList}>
<SearchInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { get, isEmpty, set, unset } from 'lodash';
import { get, has, isEmpty, set, unset } from 'lodash';
import { createContext, useState } from 'react';
import { CardFilter } from '../cardFilters/cardFilters.types';

Expand All @@ -24,13 +24,15 @@ export interface TicketFiltersContextType {
upsertFilter: (filter: CardFilter) => void;
deleteFilter: (filter: Omit<CardFilter, 'filter'>) => void;
deleteAllFilters: () => void;
hasFilter: (filter: Omit<CardFilter, 'filter'>) => boolean;
}

const defaultValue: TicketFiltersContextType = {
filters: {},
upsertFilter: () => {},
deleteFilter: () => {},
deleteAllFilters: () => {},
hasFilter: () => false,
};
export const TicketFiltersContext = createContext(defaultValue);
TicketFiltersContext.displayName = 'TicketFiltersContext';
Expand Down Expand Up @@ -67,12 +69,15 @@ export const TicketFiltersContextComponent = ({ filters: initialFilters, childre

const deleteAllFilters = () => setFilters({});

const hasFilter = ({ module, property, type }: CardFilter) => has(filters, [module, property, type]);

return (
<TicketFiltersContext.Provider value={{
filters,
upsertFilter,
deleteFilter,
deleteAllFilters,
hasFilter,
}}>
{children}
</TicketFiltersContext.Provider>
Expand Down

0 comments on commit d396a18

Please sign in to comment.