Merge branch '14277-search-view-editing-saved-queries'
[arvados-workbench2.git] / src / store / tree-picker / tree-picker-actions.ts
index 41ee62f8e1910e5fb1c274711c8138bda4489d8d..a988e8961733545dbc075c2e793bcbb537e8b968 100644 (file)
@@ -3,28 +3,70 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { unionize, ofType, UnionOf } from "~/common/unionize";
-import { TreeNode, initTreeNode, getNodeDescendants, getNodeDescendantsIds, getNodeValue, TreeNodeStatus } from '~/models/tree';
+import { TreeNode, initTreeNode, getNodeDescendants, getNodeDescendantsIds, getNodeValue, TreeNodeStatus, getNode } from '~/models/tree';
 import { Dispatch } from 'redux';
 import { RootState } from '~/store/store';
 import { ServiceRepository } from '~/services/services';
 import { FilterBuilder } from '~/services/api/filter-builder';
-import { pipe } from 'lodash/fp';
+import { pipe, map, values, mapValues } from 'lodash/fp';
 import { ResourceKind } from '~/models/resource';
 import { GroupContentsResource } from '../../services/groups-service/groups-service';
 import { CollectionDirectory, CollectionFile } from '../../models/collection-file';
+import { getTreePicker, TreePicker } from './tree-picker';
+import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
 
 export const treePickerActions = unionize({
     LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
     LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ id: string, nodes: Array<TreeNode<any>>, pickerId: string }>(),
     TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(),
     ACTIVATE_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
+    DEACTIVATE_TREE_PICKER_NODE: ofType<{ pickerId: string }>(),
     TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string }>(),
+    SELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
+    DESELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
     EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(),
     RESET_TREE_PICKER: ofType<{ pickerId: string }>()
 });
 
 export type TreePickerAction = UnionOf<typeof treePickerActions>;
 
+export const getProjectsTreePickerIds = (pickerId: string) => ({
+    home: `${pickerId}_home`,
+    shared: `${pickerId}_shared`,
+    favorites: `${pickerId}_favorites`,
+});
+
+export const getAllNodes = <Value>(pickerId: string, filter = (node: TreeNode<Value>) => true) => (state: TreePicker) =>
+    pipe(
+        () => values(getProjectsTreePickerIds(pickerId)),
+
+        ids => ids
+            .map(id => getTreePicker<Value>(id)(state)),
+
+        trees => trees
+            .map(getNodeDescendants(''))
+            .reduce((allNodes, nodes) => allNodes.concat(nodes), []),
+
+        allNodes => allNodes
+            .reduce((map, node) =>
+                filter(node)
+                    ? map.set(node.id, node)
+                    : map, new Map<string, TreeNode<Value>>())
+            .values(),
+
+        uniqueNodes => Array.from(uniqueNodes),
+    )();
+export const getSelectedNodes = <Value>(pickerId: string) => (state: TreePicker) =>
+    getAllNodes<Value>(pickerId, node => node.selected)(state);
+    
+export const initProjectsTreePicker = (pickerId: string) =>
+    async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
+        const { home, shared, favorites } = getProjectsTreePickerIds(pickerId);
+        dispatch<any>(initUserProject(home));
+        dispatch<any>(initSharedProject(shared));
+        dispatch<any>(initFavoritesProject(favorites));
+    };
+
 interface ReceiveTreePickerDataParams<T> {
     data: T[];
     extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
@@ -81,22 +123,30 @@ export const loadProject = (params: LoadProjectParams) =>
     };
 
 export const loadCollection = (id: string, pickerId: string) =>
-    async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
 
-        const files = await services.collectionService.files(id);
-        const data = getNodeDescendants('')(files).map(node => node.value);
-
-        dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
-            id,
-            pickerId,
-            data,
-            extractNodeData: value => ({
-                id: value.id,
-                status: TreeNodeStatus.LOADED,
-                value,
-            }),
-        }));
+        const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
+        if (picker) {
+
+            const node = getNode(id)(picker);
+            if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
+
+                const files = await services.collectionService.files(node.value.portableDataHash);
+                const data = getNodeDescendants('')(files).map(node => node.value);
+
+                dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
+                    id,
+                    pickerId,
+                    data,
+                    extractNodeData: value => ({
+                        id: value.id,
+                        status: TreeNodeStatus.LOADED,
+                        value,
+                    }),
+                }));
+            }
+        }
     };