Skip to content

Commit

Permalink
[Aggregate queries for table views - #1] Introduce aggregateOperation…
Browse files Browse the repository at this point in the history
…ForViewFieldState (#9010)

Introducing aggregateOperationForViewFieldState to add a state storing
the aggregate operation for each view field
  • Loading branch information
ijreilly authored Dec 10, 2024
1 parent b6e02b6 commit 8ecf07f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useSetRecordGroup } from '@/object-record/record-group/hooks/useSetReco
import { RecordIndexFiltersToContextStoreEffect } from '@/object-record/record-index/components/RecordIndexFiltersToContextStoreEffect';
import { recordIndexKanbanAggregateOperationState } from '@/object-record/record-index/states/recordIndexKanbanAggregateOperationState';
import { recordIndexViewFilterGroupsState } from '@/object-record/record-index/states/recordIndexViewFilterGroupsState';
import { aggregateOperationForViewFieldState } from '@/object-record/record-table/record-table-footer/states/aggregateOperationForViewFieldState';
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';
import { ViewBar } from '@/views/components/ViewBar';
import { ViewField } from '@/views/types/ViewField';
Expand Down Expand Up @@ -118,6 +119,25 @@ export const RecordIndexContainer = () => {
) {
set(recordIndexFieldDefinitionsState, newFieldDefinitions);
}

for (const viewField of viewFields) {
const aggregateOperationForViewField = snapshot
.getLoadable(
aggregateOperationForViewFieldState({
viewFieldId: viewField.id,
}),
)
.getValue();

if (aggregateOperationForViewField !== viewField.aggregateOperation) {
set(
aggregateOperationForViewFieldState({
viewFieldId: viewField.id,
}),
viewField.aggregateOperation,
);
}
}
},
[columnDefinitions, setTableColumns],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { RecordIndexRootPropsContext } from '@/object-record/record-index/contex
import { useHandleToggleColumnFilter } from '@/object-record/record-index/hooks/useHandleToggleColumnFilter';
import { useHandleToggleColumnSort } from '@/object-record/record-index/hooks/useHandleToggleColumnSort';
import { useRecordTable } from '@/object-record/record-table/hooks/useRecordTable';
import { aggregateOperationForViewFieldState } from '@/object-record/record-table/record-table-footer/states/aggregateOperationForViewFieldState';
import { useGetCurrentView } from '@/views/hooks/useGetCurrentView';
import { useSetRecordCountInCurrentView } from '@/views/hooks/useSetRecordCountInCurrentView';
import { ViewField } from '@/views/types/ViewField';
import { useRecoilCallback } from 'recoil';

export const RecordIndexTableContainerEffect = () => {
const { recordIndexId, objectNameSingular } = useContext(
Expand Down Expand Up @@ -48,6 +52,8 @@ export const RecordIndexTableContainerEffect = () => {
viewBarId,
});

const { currentViewWithSavedFiltersAndSorts } = useGetCurrentView();

useEffect(() => {
setOnToggleColumnFilter(
() => (fieldMetadataId: string) =>
Expand All @@ -68,5 +74,37 @@ export const RecordIndexTableContainerEffect = () => {
);
}, [setRecordCountInCurrentView, setOnEntityCountChange]);

const setViewFieldAggregateOperation = useRecoilCallback(
({ set, snapshot }) =>
(viewField: ViewField) => {
const aggregateOperationForViewField = snapshot
.getLoadable(
aggregateOperationForViewFieldState({
viewFieldId: viewField.id,
}),
)
.getValue();

if (aggregateOperationForViewField !== viewField.aggregateOperation) {
set(
aggregateOperationForViewFieldState({
viewFieldId: viewField.id,
}),
viewField.aggregateOperation,
);
}
},
[],
);

useEffect(() => {
currentViewWithSavedFiltersAndSorts?.viewFields.forEach((viewField) => {
setViewFieldAggregateOperation(viewField);
});
}, [
currentViewWithSavedFiltersAndSorts?.viewFields,
setViewFieldAggregateOperation,
]);

return <></>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AGGREGATE_OPERATIONS } from '@/object-record/record-table/constants/AggregateOperations';
import { createFamilyState } from '@/ui/utilities/state/utils/createFamilyState';

export const aggregateOperationForViewFieldState = createFamilyState<
AGGREGATE_OPERATIONS | null | undefined,
{ viewFieldId: string }
>({
key: 'aggregateOperationForViewFieldState',
defaultValue: null,
});

0 comments on commit 8ecf07f

Please sign in to comment.