Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-elias committed Oct 20, 2024
1 parent 36a019f commit 9f15d46
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useObjectMetadataItemById } from '@/object-metadata/hooks/useObjectMetadataItemById';
import { useCurrentViewViewFilterGroup } from '@/object-record/advanced-filter/hooks/useCurrentViewViewFilterGroup';
import { useUpsertCombinedViewFilterGroup } from '@/object-record/advanced-filter/hooks/useUpsertCombinedViewFilterGroup';
import { getOperandsForFilterDefinition } from '@/object-record/object-filter-dropdown/utils/getOperandsForFilterType';
import { LightButton } from '@/ui/input/button/components/LightButton';
Expand All @@ -11,31 +12,33 @@ import { ADVANCED_FILTER_DROPDOWN_ID } from '@/views/constants/AdvancedFilterDro
import { useGetCurrentView } from '@/views/hooks/useGetCurrentView';
import { useUpsertCombinedViewFilters } from '@/views/hooks/useUpsertCombinedViewFilters';
import { availableFilterDefinitionsComponentState } from '@/views/states/availableFilterDefinitionsComponentState';
import { ViewFilter } from '@/views/types/ViewFilter';
import { ViewFilterGroup } from '@/views/types/ViewFilterGroup';
import { ViewFilterGroupLogicalOperator } from '@/views/types/ViewFilterGroupLogicalOperator';
import { useCallback } from 'react';
import { IconLibraryPlus, IconPlus } from 'twenty-ui';
import { IconLibraryPlus, IconPlus, isDefined } from 'twenty-ui';
import { v4 } from 'uuid';

interface AdvancedFilterAddFilterRuleSelectProps {
viewId?: string;
currentViewFilterGroup: ViewFilterGroup;
childViewFiltersAndViewFilterGroups: (ViewFilterGroup | ViewFilter)[];
isFilterRuleGroupOptionVisible?: boolean;
parentViewFilterGroupId?: string;
}

export const AdvancedFilterAddFilterRuleSelect = (
props: AdvancedFilterAddFilterRuleSelectProps,
) => {
const dropdownId = `advanced-filter-add-filter-rule-${props.currentViewFilterGroup.id}`;
export const AdvancedFilterAddFilterRuleSelect = ({
parentViewFilterGroupId,
}: AdvancedFilterAddFilterRuleSelectProps) => {
const { currentViewFilterGroup, childViewFiltersAndViewFilterGroups } =
useCurrentViewViewFilterGroup({
parentViewFilterGroupId,
});

const dropdownId = `advanced-filter-add-filter-rule-${currentViewFilterGroup.id}`;

const { currentViewId } = useGetCurrentView();

const { upsertCombinedViewFilterGroup } = useUpsertCombinedViewFilterGroup();
const { upsertCombinedViewFilter } = useUpsertCombinedViewFilters();

const newPositionInViewFilterGroup =
(props.childViewFiltersAndViewFilterGroups[
props.childViewFiltersAndViewFilterGroups.length - 1
(childViewFiltersAndViewFilterGroups[
childViewFiltersAndViewFilterGroups.length - 1
]?.positionInViewFilterGroup ?? 0) + 1;

const { closeDropdown } = useDropdown(dropdownId);
Expand Down Expand Up @@ -78,23 +81,23 @@ export const AdvancedFilterAddFilterRuleSelect = (
definition: defaultFilterDefinition,
value: '',
displayValue: '',
viewFilterGroupId: props.currentViewFilterGroup.id,
viewFilterGroupId: currentViewFilterGroup.id,
positionInViewFilterGroup: newPositionInViewFilterGroup,
});
};

const handleAddFilterGroup = () => {
closeDropdown();

if (!props.viewId) {
if (!currentViewId) {
throw new Error('Missing view id');
}

const newViewFilterGroup = {
id: v4(),
viewId: props.viewId,
viewId: currentViewId,
logicalOperator: ViewFilterGroupLogicalOperator.AND,
parentViewFilterGroupId: props.currentViewFilterGroup.id,
parentViewFilterGroupId: currentViewFilterGroup.id,
positionInViewFilterGroup: newPositionInViewFilterGroup,
};

Expand All @@ -114,7 +117,9 @@ export const AdvancedFilterAddFilterRuleSelect = (
});
};

if (!props.isFilterRuleGroupOptionVisible) {
const isFilterRuleGroupOptionVisible = !isDefined(parentViewFilterGroupId);

if (!isFilterRuleGroupOptionVisible) {
return (
<LightButton
Icon={IconPlus}
Expand All @@ -138,7 +143,7 @@ export const AdvancedFilterAddFilterRuleSelect = (
text="Add rule"
onClick={handleAddFilter}
/>
{props.isFilterRuleGroupOptionVisible && (
{isFilterRuleGroupOptionVisible && (
<MenuItem
LeftIcon={IconLibraryPlus}
text="Add rule group"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AdvancedFilterAddFilterRuleSelect } from '@/object-record/advanced-filt
import { AdvancedFilterLogicalOperatorCell } from '@/object-record/advanced-filter/components/AdvancedFilterLogicalOperatorCell';
import { AdvancedFilterRuleOptionsDropdown } from '@/object-record/advanced-filter/components/AdvancedFilterRuleOptionsDropdown';
import { AdvancedFilterViewFilter } from '@/object-record/advanced-filter/components/AdvancedFilterViewFilter';
import { useGetCurrentView } from '@/views/hooks/useGetCurrentView';
import { useCurrentViewViewFilterGroup } from '@/object-record/advanced-filter/hooks/useCurrentViewViewFilterGroup';
import styled from '@emotion/styled';

const StyledRow = styled.div`
Expand Down Expand Up @@ -33,43 +33,17 @@ interface AdvancedFilterViewFilterGroupProps {
export const AdvancedFilterViewFilterGroup = (
props: AdvancedFilterViewFilterGroupProps,
) => {
const { currentViewWithCombinedFiltersAndSorts } = useGetCurrentView();

const viewFilters = currentViewWithCombinedFiltersAndSorts?.viewFilters;
const viewFilterGroups =
currentViewWithCombinedFiltersAndSorts?.viewFilterGroups;

const currentViewFilterGroup = viewFilterGroups?.find((viewFilterGroup) =>
props.parentViewFilterGroupId
? viewFilterGroup.parentViewFilterGroupId ===
props.parentViewFilterGroupId
: !viewFilterGroup.parentViewFilterGroupId,
);
const { currentViewFilterGroup, childViewFiltersAndViewFilterGroups } =
useCurrentViewViewFilterGroup({
parentViewFilterGroupId: props.parentViewFilterGroupId,
});

if (!currentViewFilterGroup) {
throw new Error(
`Missing component view filter group for view filter group with parent id of '${props.parentViewFilterGroupId}'`,
);
}

const childViewFilters = viewFilters?.filter(
(viewFilter) => viewFilter.viewFilterGroupId === currentViewFilterGroup.id,
);

const childViewFilterGroups = viewFilterGroups?.filter(
(viewFilterGroup) =>
viewFilterGroup.parentViewFilterGroupId === currentViewFilterGroup.id,
);

const childViewFiltersAndViewFilterGroups = [
...(childViewFilterGroups ?? []),
...(childViewFilters ?? []),
].sort((a, b) => {
const positionA = a.positionInViewFilterGroup ?? 0;
const positionB = b.positionInViewFilterGroup ?? 0;
return positionA - positionB;
});

return (
<StyledContainer isGrayBackground={!!props.parentViewFilterGroupId}>
{childViewFiltersAndViewFilterGroups?.map((child, i) =>
Expand Down Expand Up @@ -107,12 +81,7 @@ export const AdvancedFilterViewFilterGroup = (
),
)}
<AdvancedFilterAddFilterRuleSelect
viewId={currentViewWithCombinedFiltersAndSorts?.id}
currentViewFilterGroup={currentViewFilterGroup}
childViewFiltersAndViewFilterGroups={
childViewFiltersAndViewFilterGroups
}
isFilterRuleGroupOptionVisible={!props.parentViewFilterGroupId}
parentViewFilterGroupId={props.parentViewFilterGroupId}
/>
</StyledContainer>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useGetCurrentView } from '@/views/hooks/useGetCurrentView';

export const useCurrentViewViewFilterGroup = ({
parentViewFilterGroupId,
}: {
parentViewFilterGroupId?: string;
}) => {
const { currentViewWithCombinedFiltersAndSorts } = useGetCurrentView();

const currentViewFilterGroup =
currentViewWithCombinedFiltersAndSorts?.viewFilterGroups.find(
(viewFilterGroup) =>
parentViewFilterGroupId
? viewFilterGroup.parentViewFilterGroupId === parentViewFilterGroupId
: !viewFilterGroup.parentViewFilterGroupId,
);

if (!currentViewFilterGroup) {
throw new Error('Current view filter group not found');
}

const childViewFilters =
currentViewWithCombinedFiltersAndSorts?.viewFilters.filter(
(viewFilter) =>
viewFilter.viewFilterGroupId === currentViewFilterGroup.id,
);

const childViewFilterGroups =
currentViewWithCombinedFiltersAndSorts?.viewFilterGroups.filter(
(viewFilterGroup) =>
viewFilterGroup.parentViewFilterGroupId === currentViewFilterGroup.id,
);

const childViewFiltersAndViewFilterGroups = [
...(childViewFilterGroups ?? []),
...(childViewFilters ?? []),
].sort((a, b) => {
const positionA = a.positionInViewFilterGroup ?? 0;
const positionB = b.positionInViewFilterGroup ?? 0;
return positionA - positionB;
});

return { currentViewFilterGroup, childViewFiltersAndViewFilterGroups };
};

0 comments on commit 9f15d46

Please sign in to comment.