refactor: Improve existing item ID handling in BomItemEditorComponent
- Updated the logic for tracking existing item IDs to prevent duplicates during item addition, ensuring that sibling items are checked for duplicates at the same level while allowing duplicates in child levels. - Enhanced the existingItemIds calculation to differentiate between root level and child level additions, improving data integrity and user experience. - Refactored the useMemo hook to include addTargetParentId as a dependency, ensuring accurate updates when the target parent ID changes.
This commit is contained in:
parent
649bd77bbb
commit
1a6d78df43
|
|
@ -937,19 +937,38 @@ export function BomItemEditorComponent({
|
|||
setItemSearchOpen(true);
|
||||
}, []);
|
||||
|
||||
// 이미 추가된 품목 ID 목록 (중복 방지용)
|
||||
// 같은 레벨(형제) 품목 ID 목록 (동일 레벨 중복 방지, 하위 레벨은 허용)
|
||||
const existingItemIds = useMemo(() => {
|
||||
const ids = new Set<string>();
|
||||
const collect = (nodes: BomItemNode[]) => {
|
||||
for (const n of nodes) {
|
||||
const fk = n.data[cfg.dataSource?.foreignKey || "child_item_id"];
|
||||
const fkField = cfg.dataSource?.foreignKey || "child_item_id";
|
||||
|
||||
if (addTargetParentId === null) {
|
||||
// 루트 레벨 추가: 루트 노드의 형제들만 체크
|
||||
for (const n of treeData) {
|
||||
const fk = n.data[fkField];
|
||||
if (fk) ids.add(fk);
|
||||
collect(n.children);
|
||||
}
|
||||
};
|
||||
collect(treeData);
|
||||
} else {
|
||||
// 하위 추가: 해당 부모의 직속 자식들만 체크
|
||||
const findParent = (nodes: BomItemNode[]): BomItemNode | null => {
|
||||
for (const n of nodes) {
|
||||
if (n.tempId === addTargetParentId) return n;
|
||||
const found = findParent(n.children);
|
||||
if (found) return found;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const parent = findParent(treeData);
|
||||
if (parent) {
|
||||
for (const child of parent.children) {
|
||||
const fk = child.data[fkField];
|
||||
if (fk) ids.add(fk);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ids;
|
||||
}, [treeData, cfg]);
|
||||
}, [treeData, cfg, addTargetParentId]);
|
||||
|
||||
// 루트 품목 추가 시작
|
||||
const handleAddRoot = useCallback(() => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue