Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / models / collection-file.ts
index 37e18cfc0d247905900d3fec4ac6160a8d3afe01..3688557a6154c8e8a3fc9dca87d486d004a6b754 100644 (file)
@@ -3,6 +3,7 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { Tree, createTree, setNode, TreeNodeStatus } from './tree';
+import { head, split, pipe, join } from 'lodash/fp';
 
 export type CollectionFilesTree = Tree<CollectionDirectory | CollectionFile>;
 
@@ -51,7 +52,7 @@ export const createCollectionFile = (data: Partial<CollectionFile>): CollectionF
     ...data
 });
 
-export const createCollectionFilesTree = (data: Array<CollectionDirectory | CollectionFile>) => {
+export const createCollectionFilesTree = (data: Array<CollectionDirectory | CollectionFile>, joinParents: Boolean = true) => {
     const directories = data.filter(item => item.type === CollectionFileType.DIRECTORY);
     directories.sort((a, b) => a.path.localeCompare(b.path));
     const files = data.filter(item => item.type === CollectionFileType.FILE);
@@ -59,12 +60,21 @@ export const createCollectionFilesTree = (data: Array<CollectionDirectory | Coll
         .reduce((tree, item) => setNode({
             children: [],
             id: item.id,
-            parent: item.path,
+            parent: joinParents ? getParentId(item) : '',
             value: item,
             active: false,
             selected: false,
             expanded: false,
             status: TreeNodeStatus.INITIAL
-
         })(tree), createTree<CollectionDirectory | CollectionFile>());
-};
\ No newline at end of file
+};
+
+const getParentId = (item: CollectionDirectory | CollectionFile) =>
+    item.path
+        ? join('', [getCollectionResourceCollectionUuid(item.id), item.path])
+        : item.path;
+
+export const getCollectionResourceCollectionUuid = pipe(
+    split('/'),
+    head,
+);