20031: Add type for collection file operation location for form field
[arvados.git] / src / store / tree-picker / tree-picker-actions.ts
index b8003aa1c29ed045c81eb621ba62d8826bd271fc..505e0622e757e00f72b53f0d7a905d0d0e1b9394 100644 (file)
@@ -4,7 +4,7 @@
 
 import { unionize, ofType, UnionOf } from "common/unionize";
 import { TreeNode, initTreeNode, getNodeDescendants, TreeNodeStatus, getNode, TreePickerId, Tree } from 'models/tree';
-import { CollectionFileType, createCollectionFilesTree } from "models/collection-file";
+import { CollectionFileType, createCollectionFilesTree, getCollectionResourceCollectionUuid } from "models/collection-file";
 import { Dispatch } from 'redux';
 import { RootState } from 'store/store';
 import { getUserUuid } from "common/getuser";
@@ -482,3 +482,32 @@ const buildParams = (ownerUuid: string) => {
             .getOrder()
     };
 };
+
+/**
+ * Given a tree picker item, return collection uuid and path
+ *   if the item represents a valid target/destination location
+ */
+export type FileOperationLocation = {
+    uuid: string;
+    path: string;
+}
+export const getFileOperationLocation = (item: ProjectsTreePickerItem): FileOperationLocation | undefined => {
+    if ('kind' in item && item.kind === ResourceKind.COLLECTION) {
+        return {
+            uuid: item.uuid,
+            path: '/'
+        };
+    } else if ('type' in item && item.type === CollectionFileType.DIRECTORY) {
+        const uuid = getCollectionResourceCollectionUuid(item.id);
+        if (uuid) {
+            return {
+                uuid,
+                path: [item.path, item.name].join('/')
+            };
+        } else {
+            return undefined;
+        }
+    } else {
+        return undefined;
+    }
+};