From 569ad6e50eb880815d91d11c5e33ea892d8a1c00 Mon Sep 17 00:00:00 2001 From: Max Makaluk <70341920+FussuChalice@users.noreply.github.com> Date: Sun, 8 Dec 2024 12:17:08 +0200 Subject: [PATCH] fix(reports): add indeterminate state for S-21 export checkboxes --- .../active_publishers/useActivePublishers.tsx | 11 ++++- .../useFieldServiceGroups.tsx | 11 ++++- .../useInactivePublishers.tsx | 11 ++++- .../useParentUncheckHandler.tsx | 46 +++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/features/reports/publisher_records/export_S21/specific_records/useParentUncheckHandler.tsx diff --git a/src/features/reports/publisher_records/export_S21/specific_records/active_publishers/useActivePublishers.tsx b/src/features/reports/publisher_records/export_S21/specific_records/active_publishers/useActivePublishers.tsx index 804bd68eda..b19941e22b 100644 --- a/src/features/reports/publisher_records/export_S21/specific_records/active_publishers/useActivePublishers.tsx +++ b/src/features/reports/publisher_records/export_S21/specific_records/active_publishers/useActivePublishers.tsx @@ -8,11 +8,13 @@ import { fullnameOptionState } from '@states/settings'; import { buildPersonFullname } from '@utils/common'; import { ActivePublishersProps } from './index.types'; import usePersons from '@features/persons/hooks/usePersons'; +import useParentUncheckHandler from '../useParentUncheckHandler'; const useActivePublishers = ({ onExport }: ActivePublishersProps) => { const { t } = useAppTranslation(); const { getFTSMonths, getAPMonths, getPublisherMonths } = usePersons(); + const { deleteSelectionFromParentItem } = useParentUncheckHandler(); const toggledItemRef = useRef<{ [itemId: string]: boolean }>({}); @@ -157,7 +159,14 @@ const useActivePublishers = ({ onExport }: ActivePublishersProps) => { ) ); - setSelected(newSelectedItemsWithChildren); + // remove parent check if at least one child element has been unchecked. + const selectedItemsWithoutParent = deleteSelectionFromParentItem( + newSelectedItemsWithChildren, + groups, + selected + ); + + setSelected(selectedItemsWithoutParent); toggledItemRef.current = {}; }; diff --git a/src/features/reports/publisher_records/export_S21/specific_records/field_service_groups/useFieldServiceGroups.tsx b/src/features/reports/publisher_records/export_S21/specific_records/field_service_groups/useFieldServiceGroups.tsx index a8be442e54..6032d9c1df 100644 --- a/src/features/reports/publisher_records/export_S21/specific_records/field_service_groups/useFieldServiceGroups.tsx +++ b/src/features/reports/publisher_records/export_S21/specific_records/field_service_groups/useFieldServiceGroups.tsx @@ -11,11 +11,13 @@ import { FieldServiceGroupType } from '@definition/field_service_groups'; import { personsState } from '@states/persons'; import { FieldServiceGroupsProps } from './index.types'; import usePersons from '@features/persons/hooks/usePersons'; +import useParentUncheckHandler from '../useParentUncheckHandler'; const useFieldServiceGroups = ({ onExport }: FieldServiceGroupsProps) => { const { t } = useAppTranslation(); const { getPublishersActive } = usePersons(); + const { deleteSelectionFromParentItem } = useParentUncheckHandler(); const toggledItemRef = useRef<{ [itemId: string]: boolean }>({}); @@ -148,7 +150,14 @@ const useFieldServiceGroups = ({ onExport }: FieldServiceGroupsProps) => { ) ); - setSelected(newSelectedItemsWithChildren); + // remove parent check if at least one child element has been unchecked. + const selectedItemsWithoutParent = deleteSelectionFromParentItem( + newSelectedItemsWithChildren, + groups, + selected + ); + + setSelected(selectedItemsWithoutParent); toggledItemRef.current = {}; }; diff --git a/src/features/reports/publisher_records/export_S21/specific_records/inactive_publishers/useInactivePublishers.tsx b/src/features/reports/publisher_records/export_S21/specific_records/inactive_publishers/useInactivePublishers.tsx index a53dfc9c9b..d087e99645 100644 --- a/src/features/reports/publisher_records/export_S21/specific_records/inactive_publishers/useInactivePublishers.tsx +++ b/src/features/reports/publisher_records/export_S21/specific_records/inactive_publishers/useInactivePublishers.tsx @@ -8,11 +8,13 @@ import { fullnameOptionState } from '@states/settings'; import { buildPersonFullname } from '@utils/common'; import { InactivePublishersProps } from './index.types'; import usePersons from '@features/persons/hooks/usePersons'; +import useParentUncheckHandler from '../useParentUncheckHandler'; const useInactivePublishers = ({ onExport }: InactivePublishersProps) => { const { t } = useAppTranslation(); const { getPublishersInactive } = usePersons(); + const { deleteSelectionFromParentItem } = useParentUncheckHandler(); const toggledItemRef = useRef<{ [itemId: string]: boolean }>({}); @@ -113,7 +115,14 @@ const useInactivePublishers = ({ onExport }: InactivePublishersProps) => { ) ); - setSelected(newSelectedItemsWithChildren); + // remove parent check if at least one child element has been unchecked. + const selectedItemsWithoutParent = deleteSelectionFromParentItem( + newSelectedItemsWithChildren, + groups, + selected + ); + + setSelected(selectedItemsWithoutParent); toggledItemRef.current = {}; }; diff --git a/src/features/reports/publisher_records/export_S21/specific_records/useParentUncheckHandler.tsx b/src/features/reports/publisher_records/export_S21/specific_records/useParentUncheckHandler.tsx new file mode 100644 index 0000000000..4159ecee1f --- /dev/null +++ b/src/features/reports/publisher_records/export_S21/specific_records/useParentUncheckHandler.tsx @@ -0,0 +1,46 @@ +import { TreeViewBaseItem } from '@mui/x-tree-view'; + +const useParentUncheckHandler = () => { + const findParentIdByItem = ( + dataSource: TreeViewBaseItem[], + itemId: string, + parentId: string = null + ) => { + for (const item of dataSource) { + if (item.id === itemId) { + return parentId; + } + if (item.children) { + const found = findParentIdByItem(item.children, itemId, item.id); + if (found) { + return found; + } + } + } + return null; + }; + + const deleteSelectionFromParentItem = ( + oldSelectedList: string[], + groups: TreeViewBaseItem[], + selected: string[] + ) => { + const missedItem = selected.filter( + (item) => !oldSelectedList.includes(item) + )[0]; + + if (!missedItem) { + return oldSelectedList; + } + + const missedItemParent = findParentIdByItem(groups, missedItem); + + return oldSelectedList.filter((item) => item !== missedItemParent); + }; + + return { + deleteSelectionFromParentItem, + }; +}; + +export default useParentUncheckHandler;