Skip to content

Commit

Permalink
fix(reports): add indeterminate state for S-21 export checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
FussuChalice authored Dec 8, 2024
1 parent 9c5efc6 commit 569ad6e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }>({});

Expand Down Expand Up @@ -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 = {};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }>({});

Expand Down Expand Up @@ -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 = {};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }>({});

Expand Down Expand Up @@ -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 = {};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 569ad6e

Please sign in to comment.