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 multi select #8682

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -22,7 +22,6 @@ export const AdvancedFilterViewFilterValueInput = ({
const {
setFilterDefinitionUsedInDropdown,
setSelectedOperandInDropdown,
setIsObjectFilterDropdownOperandSelectUnfolded,
setSelectedFilter,
} = useFilterDropdown();

Expand Down Expand Up @@ -53,7 +52,6 @@ export const AdvancedFilterViewFilterValueInput = ({
onOpen={() => {
setFilterDefinitionUsedInDropdown(filter.definition);
setSelectedOperandInDropdown(filter.operand);
setIsObjectFilterDropdownOperandSelectUnfolded(true);
setSelectedFilter(filter);
}}
dropdownComponents={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ type MultipleFiltersDropdownButtonProps = {
export const MultipleFiltersDropdownButton = ({
hotkeyScope,
}: MultipleFiltersDropdownButtonProps) => {
const { resetFilter, setIsObjectFilterDropdownOperandSelectUnfolded } =
useFilterDropdown();
const { resetFilter } = useFilterDropdown();

const handleDropdownClose = useCallback(() => {
resetFilter();
setIsObjectFilterDropdownOperandSelectUnfolded(false);
}, [resetFilter, setIsObjectFilterDropdownOperandSelectUnfolded]);
}, [resetFilter]);

return (
<Dropdown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const ObjectFilterDropdownDateInput = () => {
filterDefinitionUsedInDropdownState,
selectedOperandInDropdownState,
selectedFilterState,
setIsObjectFilterDropdownUnfolded,
selectFilter,
} = useFilterDropdown();

Expand Down Expand Up @@ -65,8 +64,6 @@ export const ObjectFilterDropdownDateInput = () => {
definition: filterDefinitionUsedInDropdown,
viewFilterGroupId: selectedFilter?.viewFilterGroupId,
});

setIsObjectFilterDropdownUnfolded(false);
};

const handleRelativeDateChange = (
Expand Down Expand Up @@ -95,8 +92,6 @@ export const ObjectFilterDropdownDateInput = () => {
definition: filterDefinitionUsedInDropdown,
viewFilterGroupId: selectedFilter?.viewFilterGroupId,
});

setIsObjectFilterDropdownUnfolded(false);
};

const isRelativeOperand =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,8 @@ export const ObjectFilterDropdownFilterInput = ({
const {
filterDefinitionUsedInDropdownState,
selectedOperandInDropdownState,
isObjectFilterDropdownOperandSelectUnfoldedState,
} = useFilterDropdown({ filterDropdownId });

const isObjectFilterDropdownOperandSelectUnfolded = useRecoilValue(
isObjectFilterDropdownOperandSelectUnfoldedState,
);

const filterDefinitionUsedInDropdown = useRecoilValue(
filterDefinitionUsedInDropdownState,
);
Expand All @@ -58,9 +53,7 @@ export const ObjectFilterDropdownFilterInput = ({
ViewFilterOperand.IsRelative,
].includes(selectedOperandInDropdown);

const shouldHide = isObjectFilterDropdownOperandSelectUnfolded;

if (shouldHide || !isDefined(filterDefinitionUsedInDropdown)) {
if (!isDefined(filterDefinitionUsedInDropdown)) {
return null;
}

Expand All @@ -84,6 +77,7 @@ export const ObjectFilterDropdownFilterInput = ({
{filterDefinitionUsedInDropdown.type === 'RELATION' && (
<>
<ObjectFilterDropdownSearchInput />
<DropdownMenuSeparator />
<ObjectFilterDropdownRecordSelect />
</>
)}
Expand All @@ -98,6 +92,7 @@ export const ObjectFilterDropdownFilterInput = ({
) && (
<>
<ObjectFilterDropdownSearchInput />
<DropdownMenuSeparator />
<ObjectFilterDropdownOptionSelect />
</>
)}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useRecoilValue } from 'recoil';
import { IconChevronDown } from 'twenty-ui';

import { useFilterDropdown } from '@/object-record/object-filter-dropdown/hooks/useFilterDropdown';
import { DropdownMenuHeader } from '@/ui/layout/dropdown/components/DropdownMenuHeader';

import { ObjectFilterDropdownOperandSelect } from '@/object-record/object-filter-dropdown/components/ObjectFilterDropdownOperandSelect';
import { FiltersHotkeyScope } from '@/object-record/object-filter-dropdown/types/FiltersHotkeyScope';
import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown';
import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { getOperandLabel } from '../utils/getOperandLabel';

const StyledDropdownMenuHeader = styled(DropdownMenuHeader)`
cursor: pointer;
`;

export const ObjectFilterDropdownOperandDropdown = ({
filterDropdownId,
}: {
filterDropdownId?: string;
}) => {
const { selectedOperandInDropdownState } = useFilterDropdown();
bosiraphael marked this conversation as resolved.
Show resolved Hide resolved

const selectedOperandInDropdown = useRecoilValue(
selectedOperandInDropdownState,
);

const theme = useTheme();

return (
<Dropdown
dropdownId={`${filterDropdownId}-operand-dropdown`}
clickableComponent={
bosiraphael marked this conversation as resolved.
Show resolved Hide resolved
<StyledDropdownMenuHeader
key={'selected-filter-operand'}
EndIcon={IconChevronDown}
>
{getOperandLabel(selectedOperandInDropdown)}
</StyledDropdownMenuHeader>
}
dropdownComponents={<ObjectFilterDropdownOperandSelect />}
bosiraphael marked this conversation as resolved.
Show resolved Hide resolved
dropdownHotkeyScope={{
scope: FiltersHotkeyScope.ObjectFilterDropdownOperandDropdown,
}}
dropdownOffset={{
x: parseInt(theme.spacing(2)),
bosiraphael marked this conversation as resolved.
Show resolved Hide resolved
bosiraphael marked this conversation as resolved.
Show resolved Hide resolved
}}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@ import { useRecoilValue } from 'recoil';
import { v4 } from 'uuid';

import { useFilterDropdown } from '@/object-record/object-filter-dropdown/hooks/useFilterDropdown';
import { getInitialFilterValue } from '@/object-record/object-filter-dropdown/utils/getInitialFilterValue';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown';
import { ViewFilterOperand } from '@/views/types/ViewFilterOperand';
import styled from '@emotion/styled';
import { MenuItem } from 'twenty-ui';
import { isDefined } from '~/utils/isDefined';

import { getInitialFilterValue } from '@/object-record/object-filter-dropdown/utils/getInitialFilterValue';
import { getOperandLabel } from '../utils/getOperandLabel';
import { getOperandsForFilterDefinition } from '../utils/getOperandsForFilterType';

const StyledDropdownMenuItemsContainer = styled(DropdownMenuItemsContainer)`
background-color: ${({ theme }) => theme.background.primary};
border-radius: ${({ theme }) => theme.border.radius.md};
`;

export const ObjectFilterDropdownOperandSelect = () => {
const {
filterDefinitionUsedInDropdownState,
setSelectedOperandInDropdown,
isObjectFilterDropdownOperandSelectUnfoldedState,
setIsObjectFilterDropdownOperandSelectUnfolded,
selectedFilterState,
selectFilter,
} = useFilterDropdown();

const { closeDropdown } = useDropdown();

const filterDefinitionUsedInDropdown = useRecoilValue(
filterDefinitionUsedInDropdownState,
);

const isObjectFilterDropdownOperandSelectUnfolded = useRecoilValue(
isObjectFilterDropdownOperandSelectUnfoldedState,
);

const selectedFilter = useRecoilValue(selectedFilterState);

const operandsForFilterType = isDefined(filterDefinitionUsedInDropdown)
Expand All @@ -45,7 +47,6 @@ export const ObjectFilterDropdownOperandSelect = () => {
].includes(newOperand);

setSelectedOperandInDropdown(newOperand);
setIsObjectFilterDropdownOperandSelectUnfolded(false);

if (isValuelessOperand && isDefined(filterDefinitionUsedInDropdown)) {
selectFilter?.({
Expand Down Expand Up @@ -81,21 +82,18 @@ export const ObjectFilterDropdownOperandSelect = () => {
}
};

if (!isObjectFilterDropdownOperandSelectUnfolded) {
return <></>;
}

return (
<DropdownMenuItemsContainer>
<StyledDropdownMenuItemsContainer>
{operandsForFilterType.map((filterOperand, index) => (
<MenuItem
key={`select-filter-operand-${index}`}
onClick={() => {
handleOperandChange(filterOperand);
closeDropdown();
}}
text={getOperandLabel(filterOperand)}
/>
))}
bosiraphael marked this conversation as resolved.
Show resolved Hide resolved
</DropdownMenuItemsContainer>
</StyledDropdownMenuItemsContainer>
);
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ObjectFilterDropdownFilterInput } from '@/object-record/object-filter-dropdown/components/ObjectFilterDropdownFilterInput';
import { ObjectFilterDropdownFilterOperandSelect } from '@/object-record/object-filter-dropdown/components/ObjectFilterDropdownFilterOperandSelect';
import { ObjectFilterDropdownOperandDropdown } from '@/object-record/object-filter-dropdown/components/ObjectFilterDropdownOperandDropdown';

type ObjectFilterOperandSelectAndInputProps = {
filterDropdownId?: string;
Expand All @@ -10,7 +10,7 @@ export const ObjectFilterOperandSelectAndInput = ({
}: ObjectFilterOperandSelectAndInputProps) => {
return (
<>
<ObjectFilterDropdownFilterOperandSelect
<ObjectFilterDropdownOperandDropdown
filterDropdownId={filterDropdownId}
/>
<ObjectFilterDropdownFilterInput filterDropdownId={filterDropdownId} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,64 +259,6 @@ describe('useFilterDropdown', () => {
});
});

it('should set isObjectFilterDropdownOperandSelectUnfolded', async () => {
const { result } = renderHook(() => {
useFilterDropdown({ filterDropdownId });
const { isObjectFilterDropdownOperandSelectUnfoldedState } =
useFilterDropdownStates(filterDropdownId);

const [
isObjectFilterDropdownOperandSelectUnfolded,
setIsObjectFilterDropdownOperandSelectUnfolded,
] = useRecoilState(isObjectFilterDropdownOperandSelectUnfoldedState);
return {
isObjectFilterDropdownOperandSelectUnfolded,
setIsObjectFilterDropdownOperandSelectUnfolded,
};
}, renderHookConfig);

expect(result.current.isObjectFilterDropdownOperandSelectUnfolded).toBe(
false,
);

act(() => {
result.current.setIsObjectFilterDropdownOperandSelectUnfolded(true);
});

await waitFor(() => {
expect(result.current.isObjectFilterDropdownOperandSelectUnfolded).toBe(
true,
);
});
});

it('should set isObjectFilterDropdownUnfolded', async () => {
const { result } = renderHook(() => {
useFilterDropdown({ filterDropdownId });
const { isObjectFilterDropdownUnfoldedState } =
useFilterDropdownStates(filterDropdownId);

const [
isObjectFilterDropdownUnfolded,
setIsObjectFilterDropdownUnfolded,
] = useRecoilState(isObjectFilterDropdownUnfoldedState);
return {
isObjectFilterDropdownUnfolded,
setIsObjectFilterDropdownUnfolded,
};
}, renderHookConfig);

expect(result.current.isObjectFilterDropdownUnfolded).toBe(false);

act(() => {
result.current.setIsObjectFilterDropdownUnfolded(true);
});

await waitFor(() => {
expect(result.current.isObjectFilterDropdownUnfolded).toBe(true);
});
});

it('should reset filter', async () => {
const { result } = renderHook(() => {
const { resetFilter, selectFilter } = useFilterDropdown({
Expand Down
Loading
Loading