Skip to content

Commit 2d32e11

Browse files
authored
Fix renamed element selection (#558)
* fix(DirectoryContent): renaming an element added the activeElement into the selected rows state. Signed-off-by: sBouzols <[email protected]>
1 parent ebdf031 commit 2d32e11

File tree

2 files changed

+19
-36
lines changed

2 files changed

+19
-36
lines changed

src/components/directory-content.tsx

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -383,24 +383,12 @@ export default function DirectoryContent() {
383383
[childrenMetadata, dispatch, getStudyUrl, handleError, intl, selectedDirectory?.elementUuid]
384384
);
385385

386-
useEffect(() => {
387-
if (!selectedDirectory?.elementUuid) {
388-
return;
389-
}
390-
setIsMissingDataAfterDirChange(true);
391-
setCheckedRows([]);
392-
}, [selectedDirectory?.elementUuid]);
393-
394-
useEffect(() => {
395-
setIsMissingDataAfterDirChange(false);
396-
}, [childrenMetadata]); // this will change after switching selectedDirectory
397-
398386
const isActiveElementUnchecked = useMemo(
399387
() => activeElement && !checkedRows.find((children) => children.elementUuid === activeElement.elementUuid),
400388
[activeElement, checkedRows]
401389
);
402390

403-
const handleRowSelected = useCallback(() => {
391+
const updateCheckedRows = useCallback(() => {
404392
setCheckedRows(computeCheckedElements(gridRef, childrenMetadata));
405393
}, [childrenMetadata]);
406394

@@ -480,7 +468,7 @@ export default function DirectoryContent() {
480468
gridRef={gridRef}
481469
rows={rows}
482470
handleCellContextualMenu={onCellContextMenu}
483-
handleRowSelected={handleRowSelected}
471+
handleRowSelected={updateCheckedRows}
484472
handleCellClick={handleCellClick}
485473
colDef={getColumnsDefinition(childrenMetadata, intl)}
486474
getRowStyle={getRowStyle}
@@ -606,6 +594,21 @@ export default function DirectoryContent() {
606594
}
607595
};
608596

597+
useEffect(() => {
598+
if (!selectedDirectory?.elementUuid) {
599+
return;
600+
}
601+
setIsMissingDataAfterDirChange(true);
602+
setCheckedRows([]);
603+
}, [selectedDirectory?.elementUuid]);
604+
605+
useEffect(() => {
606+
setIsMissingDataAfterDirChange(false);
607+
// update checkecRows ElementAttributes objects if metadata changed
608+
// ex: when the user renames a selected element
609+
updateCheckedRows();
610+
}, [childrenMetadata, updateCheckedRows]); // this will change after switching selectedDirectory
611+
609612
return (
610613
<>
611614
{
@@ -640,7 +643,6 @@ export default function DirectoryContent() {
640643
<ContentContextualMenu
641644
activeElement={activeElement}
642645
selectedElements={fullSelection}
643-
onUpdateSelectedElements={setCheckedRows}
644646
open={openContentMenu}
645647
openDialog={openDialog}
646648
setOpenDialog={setOpenDialog}

src/components/menus/content-contextual-menu.tsx

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ import { AppState } from '../../redux/types';
6161
export interface ContentContextualMenuProps {
6262
activeElement: ElementAttributes;
6363
selectedElements: ElementAttributes[];
64-
onUpdateSelectedElements: (elements: ElementAttributes[]) => void;
6564
open: boolean;
6665
onClose: () => void;
6766
openDialog: string;
@@ -72,17 +71,8 @@ export interface ContentContextualMenuProps {
7271
}
7372

7473
export default function ContentContextualMenu(props: Readonly<ContentContextualMenuProps>) {
75-
const {
76-
activeElement,
77-
selectedElements,
78-
onUpdateSelectedElements,
79-
open,
80-
onClose,
81-
openDialog,
82-
setOpenDialog,
83-
broadcastChannel,
84-
...others
85-
} = props;
74+
const { activeElement, selectedElements, open, onClose, openDialog, setOpenDialog, broadcastChannel, ...others } =
75+
props;
8676
const userId = useSelector((state: AppState) => state.user?.profile.sub);
8777
const intl = useIntl();
8878
const dispatch = useDispatch();
@@ -355,14 +345,6 @@ export default function ContentContextualMenu(props: Readonly<ContentContextualM
355345
nameItem: renamedElement[1],
356346
});
357347
}
358-
// update selected elements if element is renamed
359-
const updatedSelectedElements = selectedElements.map((element) => {
360-
if (element.elementUuid === renamedElement[0]) {
361-
return { ...element, elementName: renamedElement[1] };
362-
}
363-
return element;
364-
});
365-
onUpdateSelectedElements(updatedSelectedElements);
366348

367349
handleCloseDialog();
368350
},
@@ -760,6 +742,5 @@ export default function ContentContextualMenu(props: Readonly<ContentContextualM
760742
}
761743

762744
ContentContextualMenu.propTypes = {
763-
onUpdateSelectedElements: PropTypes.func,
764745
onClose: PropTypes.func,
765746
};

0 commit comments

Comments
 (0)