|
@@ -47,8 +47,16 @@ const CategoryTree = ({ elements, saveCategory }) => {
|
|
|
throw new Error("No source");
|
|
|
|
|
|
if (sourceCat.parent?._id !== targetCat?._id) {
|
|
|
- sourceCat.parent = targetCat ? { _id: targetCat._id, name: targetCat.name } : null;
|
|
|
- saveCategory(sourceCat);
|
|
|
+ let parentCat = params.dragSource.parentCat;
|
|
|
+ if (parentCat)
|
|
|
+ parentCat.subCategories = parentCat.subCategories.filter(sc => sc?.cat._id !== sourceCat._id);
|
|
|
+ if (!params.dropTarget.subCategories)
|
|
|
+ params.dropTarget.subCategories = [];
|
|
|
+ params.dropTarget.subCategories.push(params.dragSource);
|
|
|
+ sourceCat.parentCat = params.dropTarget;
|
|
|
+
|
|
|
+ sourceCat.parent = targetCat ?? null;
|
|
|
+ //saveCategory(sourceCat);
|
|
|
setTreeData(newTree);
|
|
|
}
|
|
|
}
|
|
@@ -61,6 +69,7 @@ const CategoryTree = ({ elements, saveCategory }) => {
|
|
|
style={{ listStyleType: 'none', paddingLeft: '0px', height: 240, flexGrow: 1, maxWidth: 400, overflowY: 'auto' }}
|
|
|
tree={treeData}
|
|
|
rootId={0}
|
|
|
+ initialOpen={[1]}
|
|
|
render={(node, { depth, isOpen, onToggle }) => (
|
|
|
<CategoryTreeItem
|
|
|
node={node}
|
|
@@ -99,10 +108,14 @@ function wrapToTreeItems(cats, parentCat = null, catTreeItems = undefined) {
|
|
|
let catTreeItem = {
|
|
|
"id": index++,
|
|
|
"parent": parentCat?.id ?? 1,
|
|
|
+ "parentCat": parentCat,
|
|
|
"droppable": true,
|
|
|
"text": cat.name,
|
|
|
- "cat": { _id: cat._id, name: cat.name, parent: parentCat?.cat ?? null }
|
|
|
+ "cat": { _id: cat._id, name: cat.name, parent: parentCat?.cat ?? null },
|
|
|
};
|
|
|
+ if (!parentCat.subCategories)
|
|
|
+ parentCat.subCategories = [];
|
|
|
+ parentCat.subCategories.push(catTreeItem);
|
|
|
catTreeItems.push(catTreeItem);
|
|
|
wrapToTreeItems(cat.subCategories, catTreeItem, catTreeItems)
|
|
|
}
|