Skip to content

Commit

Permalink
Add the support of Empty and Non-Empty filter (twentyhq#5773)
Browse files Browse the repository at this point in the history
  • Loading branch information
pacyL2K19 authored Jun 20, 2024
1 parent 9e08445 commit 9228667
Show file tree
Hide file tree
Showing 11 changed files with 478 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export type UUIDFilter = {
is?: IsFilter;
};

export type RelationFilter = {
is?: IsFilter;
in?: UUIDFilterValue[];
};

export type BooleanFilter = {
eq?: boolean;
is?: IsFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRecoilValue } from 'recoil';
import { ObjectFilterDropdownSearchInput } from '@/object-record/object-filter-dropdown/components/ObjectFilterDropdownSearchInput';
import { useFilterDropdown } from '@/object-record/object-filter-dropdown/hooks/useFilterDropdown';
import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator';
import { ViewFilterOperand } from '@/views/types/ViewFilterOperand';

import { MultipleFiltersDropdownFilterOnFilterChangedEffect } from './MultipleFiltersDropdownFilterOnFilterChangedEffect';
import { ObjectFilterDropdownDateInput } from './ObjectFilterDropdownDateInput';
Expand Down Expand Up @@ -36,13 +37,20 @@ export const MultipleFiltersDropdownContent = ({
const selectedOperandInDropdown = useRecoilValue(
selectedOperandInDropdownState,
);
const isEmptyOperand =
selectedOperandInDropdown &&
[ViewFilterOperand.IsEmpty, ViewFilterOperand.IsNotEmpty].includes(
selectedOperandInDropdown,
);

return (
<>
{!filterDefinitionUsedInDropdown ? (
<ObjectFilterDropdownFilterSelect />
) : isObjectFilterDropdownOperandSelectUnfolded ? (
<ObjectFilterDropdownOperandSelect />
) : isEmptyOperand ? (
<ObjectFilterDropdownOperandButton />
) : (
selectedOperandInDropdown && (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useRecoilValue } from 'recoil';
import { v4 } from 'uuid';

import { useFilterDropdown } from '@/object-record/object-filter-dropdown/hooks/useFilterDropdown';
import { FilterDefinition } from '@/object-record/object-filter-dropdown/types/FilterDefinition';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
import { ViewFilterOperand } from '@/views/types/ViewFilterOperand';
Expand Down Expand Up @@ -34,10 +35,27 @@ export const ObjectFilterDropdownOperandSelect = () => {
filterDefinitionUsedInDropdown?.type,
);

const handleOperangeChange = (newOperand: ViewFilterOperand) => {
const handleOperandChange = (newOperand: ViewFilterOperand) => {
const isEmptyOperand = [
ViewFilterOperand.IsEmpty,
ViewFilterOperand.IsNotEmpty,
].includes(newOperand);

setSelectedOperandInDropdown(newOperand);
setIsObjectFilterDropdownOperandSelectUnfolded(false);

if (isEmptyOperand) {
selectFilter?.({
id: v4(),
fieldMetadataId: filterDefinitionUsedInDropdown?.fieldMetadataId ?? '',
displayValue: '',
operand: newOperand,
value: '',
definition: filterDefinitionUsedInDropdown as FilterDefinition,
});
return;
}

if (
isDefined(filterDefinitionUsedInDropdown) &&
isDefined(selectedFilter)
Expand All @@ -63,7 +81,7 @@ export const ObjectFilterDropdownOperandSelect = () => {
<MenuItem
key={`select-filter-operand-${index}`}
onClick={() => {
handleOperangeChange(filterOperand);
handleOperandChange(filterOperand);
}}
text={getOperandLabel(filterOperand)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,34 @@ import { ViewFilterOperand } from '@/views/types/ViewFilterOperand';
import { getOperandsForFilterType } from '../getOperandsForFilterType';

describe('getOperandsForFilterType', () => {
const emptyOperands = [
ViewFilterOperand.IsEmpty,
ViewFilterOperand.IsNotEmpty,
];

const containsOperands = [
ViewFilterOperand.Contains,
ViewFilterOperand.DoesNotContain,
];

const numberOperands = [
ViewFilterOperand.GreaterThan,
ViewFilterOperand.LessThan,
];

const relationOperand = [ViewFilterOperand.Is, ViewFilterOperand.IsNot];

const testCases = [
['TEXT', [ViewFilterOperand.Contains, ViewFilterOperand.DoesNotContain]],
['EMAIL', [ViewFilterOperand.Contains, ViewFilterOperand.DoesNotContain]],
[
'FULL_NAME',
[ViewFilterOperand.Contains, ViewFilterOperand.DoesNotContain],
],
['ADDRESS', [ViewFilterOperand.Contains, ViewFilterOperand.DoesNotContain]],
['LINK', [ViewFilterOperand.Contains, ViewFilterOperand.DoesNotContain]],
['LINKS', [ViewFilterOperand.Contains, ViewFilterOperand.DoesNotContain]],
['CURRENCY', [ViewFilterOperand.GreaterThan, ViewFilterOperand.LessThan]],
['NUMBER', [ViewFilterOperand.GreaterThan, ViewFilterOperand.LessThan]],
['DATE_TIME', [ViewFilterOperand.GreaterThan, ViewFilterOperand.LessThan]],
['RELATION', [ViewFilterOperand.Is, ViewFilterOperand.IsNot]],
['TEXT', [...containsOperands, ...emptyOperands]],
['EMAIL', [...containsOperands, ...emptyOperands]],
['FULL_NAME', [...containsOperands, ...emptyOperands]],
['ADDRESS', [...containsOperands, ...emptyOperands]],
['LINK', [...containsOperands, ...emptyOperands]],
['LINKS', [...containsOperands, ...emptyOperands]],
['CURRENCY', [...numberOperands, ...emptyOperands]],
['NUMBER', [...numberOperands, ...emptyOperands]],
['DATE_TIME', [...numberOperands, ...emptyOperands]],
['RELATION', [...relationOperand, ...emptyOperands]],
[undefined, []],
[null, []],
['UNKNOWN_TYPE', []],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const getOperandLabel = (
return 'Is not';
case ViewFilterOperand.IsNotNull:
return 'Is not null';
case ViewFilterOperand.IsEmpty:
return 'Is empty';
case ViewFilterOperand.IsNotEmpty:
return 'Is not empty';
default:
return '';
}
Expand All @@ -35,6 +39,10 @@ export const getOperandLabelShort = (
return ': Not';
case ViewFilterOperand.IsNotNull:
return ': NotNull';
case ViewFilterOperand.IsNotEmpty:
return ': NotEmpty';
case ViewFilterOperand.IsEmpty:
return ': Empty';
case ViewFilterOperand.GreaterThan:
return '\u00A0> ';
case ViewFilterOperand.LessThan:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,39 @@ import { FilterType } from '../types/FilterType';
export const getOperandsForFilterType = (
filterType: FilterType | null | undefined,
): ViewFilterOperand[] => {
const emptyOperands = [
ViewFilterOperand.IsEmpty,
ViewFilterOperand.IsNotEmpty,
];

const relationOperands = [ViewFilterOperand.Is, ViewFilterOperand.IsNot];

switch (filterType) {
case 'TEXT':
case 'EMAIL':
case 'FULL_NAME':
case 'ADDRESS':
case 'PHONE':
case 'LINK':
return [ViewFilterOperand.Contains, ViewFilterOperand.DoesNotContain];
case 'LINKS':
return [ViewFilterOperand.Contains, ViewFilterOperand.DoesNotContain];
return [
ViewFilterOperand.Contains,
ViewFilterOperand.DoesNotContain,
...emptyOperands,
];
case 'CURRENCY':
case 'NUMBER':
case 'DATE_TIME':
case 'DATE':
return [ViewFilterOperand.GreaterThan, ViewFilterOperand.LessThan];
return [
ViewFilterOperand.GreaterThan,
ViewFilterOperand.LessThan,
...emptyOperands,
];
case 'RELATION':
return [...relationOperands, ...emptyOperands];
case 'SELECT':
return [ViewFilterOperand.Is, ViewFilterOperand.IsNot];
return [...relationOperands];
default:
return [];
}
Expand Down
Loading

0 comments on commit 9228667

Please sign in to comment.