Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 14433_properties_i...
[arvados-workbench2.git] / src / models / tree.ts
index f0b53b46f46fd03906cfd81cde9ce63f45a0028a..fe52a97b0fcdd3806579318d968f2efc8206f364 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { pipe } from 'lodash/fp';
+import { pipe, map, reduce } from 'lodash/fp';
 export type Tree<T> = Record<string, TreeNode<T>>;
 
 export const TREE_ROOT_ID = '';
@@ -24,10 +24,23 @@ export enum TreeNodeStatus {
     LOADED = 'LOADED',
 }
 
+export enum TreePickerId {
+    PROJECTS = 'Projects',
+    SHARED_WITH_ME = 'Shared with me',
+    FAVORITES = 'Favorites'
+}
+
 export const createTree = <T>(): Tree<T> => ({});
 
 export const getNode = (id: string) => <T>(tree: Tree<T>): TreeNode<T> | undefined => tree[id];
 
+export const appendSubtree = <T>(id: string, subtree: Tree<T>) => (tree: Tree<T>) =>
+    pipe(
+        getNodeDescendants(''),
+        map(node => node.parent === '' ? { ...node, parent: id } : node),
+        reduce((newTree, node) => setNode(node)(newTree), tree)
+    )(subtree) as Tree<T>;
+
 export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
     return pipe(
         (tree: Tree<T>) => getNode(node.id)(tree) === node
@@ -86,7 +99,7 @@ export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree:
     const node = getNode(id)(tree);
     const children = node ? node.children :
         id === TREE_ROOT_ID
-            ? getRootNodeChildren(tree)
+            ? getRootNodeChildrenIds(tree)
             : [];
 
     return children
@@ -207,11 +220,12 @@ const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
     ({ ...node, value: mapFn(node.value) });
 
-const getRootNodeChildren = <T>(tree: Tree<T>) =>
+const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
     Object
         .keys(tree)
         .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
 
+
 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
     const node = getNode(parentId)(tree);
     if (node) {