X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/63f23fab5d96cbf09751119c9c07d10b4d9c07cf..ba27bdf218da8894df4ffa6174c67c1fb3e70c81:/src/views-components/project-tree-picker/project-tree-picker.tsx

diff --git a/src/views-components/project-tree-picker/project-tree-picker.tsx b/src/views-components/project-tree-picker/project-tree-picker.tsx
index 86795295..a4e4c406 100644
--- a/src/views-components/project-tree-picker/project-tree-picker.tsx
+++ b/src/views-components/project-tree-picker/project-tree-picker.tsx
@@ -9,45 +9,45 @@ import { Typography } from "@material-ui/core";
 import { TreePicker, TreePickerProps } from "../tree-picker/tree-picker";
 import { TreeItem, TreeItemStatus } from "~/components/tree/tree";
 import { ProjectResource } from "~/models/project";
-import { treePickerActions } from "~/store/tree-picker/tree-picker-actions";
+import { treePickerActions, loadProjectTreePickerProjects, loadFavoriteTreePickerProjects } from "~/store/tree-picker/tree-picker-actions";
 import { ListItemTextIcon } from "~/components/list-item-text-icon/list-item-text-icon";
 import { ProjectIcon, FavoriteIcon, ProjectsIcon, ShareMeIcon } from "~/components/icon/icon";
-import { createTreePickerNode } from "~/store/tree-picker/tree-picker";
 import { RootState } from "~/store/store";
 import { ServiceRepository } from "~/services/services";
-import { FilterBuilder } from "~/services/api/filter-builder";
 import { WrappedFieldProps } from 'redux-form';
+import { TreePickerId } from '~/models/tree';
 
-type ProjectTreePickerProps = Pick<TreePickerProps, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen'>;
+type ProjectTreePickerProps = Pick<TreePickerProps<ProjectResource>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
 
 const mapDispatchToProps = (dispatch: Dispatch, props: { onChange: (projectUuid: string) => void }): ProjectTreePickerProps => ({
     onContextMenu: () => { return; },
-    toggleItemActive: (nodeId, status, pickerId) => {
-        if (nodeId !== TreePickerId.FAVORITES && nodeId !== TreePickerId.SHARED_WITH_ME) {
-            getNotSelectedTreePickerKind(pickerId)
-                .forEach(pickerId => dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId: '', pickerId })));
-            dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId, pickerId }));
+    toggleItemActive: (_, { id }, pickerId) => {
+        getNotSelectedTreePickerKind(pickerId)
+            .forEach(pickerId => dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: '', pickerId })));
+        dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId }));
 
-            props.onChange(nodeId);
-        }
+        props.onChange(id);
+    },
+    toggleItemOpen: (_, { id, status }, pickerId) => {
+        dispatch<any>(toggleItemOpen(id, status, pickerId));
+    },
+    toggleItemSelection: (_, { id }, pickerId) => {
+        dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id, pickerId }));
     },
-    toggleItemOpen: (nodeId, status, pickerId) => {
-        dispatch<any>(toggleItemOpen(nodeId, status, pickerId));
-    }
 });
 
-const toggleItemOpen = (nodeId: string, status: TreeItemStatus, pickerId: string) =>
+const toggleItemOpen = (id: string, status: TreeItemStatus, pickerId: string) =>
     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         if (status === TreeItemStatus.INITIAL) {
             if (pickerId === TreePickerId.PROJECTS) {
-                dispatch<any>(loadProjectTreePickerProjects(nodeId));
+                dispatch<any>(loadProjectTreePickerProjects(id));
             } else if (pickerId === TreePickerId.FAVORITES) {
-                dispatch<any>(loadFavoriteTreePickerProjects(nodeId === services.authService.getUuid() ? '' : nodeId));
+                dispatch<any>(loadFavoriteTreePickerProjects(id === services.authService.getUuid() ? '' : id));
             } else {
                 // TODO: load sharedWithMe
             }
         } else {
-            dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId }));
+            dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
         }
     };
 
@@ -55,12 +55,6 @@ const getNotSelectedTreePickerKind = (pickerId: string) => {
     return [TreePickerId.PROJECTS, TreePickerId.FAVORITES, TreePickerId.SHARED_WITH_ME].filter(nodeId => nodeId !== pickerId);
 };
 
-export enum TreePickerId {
-    PROJECTS = 'Projects',
-    SHARED_WITH_ME = 'Shared with me',
-    FAVORITES = 'Favorites'
-}
-
 export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props: ProjectTreePickerProps) =>
     <div style={{ display: 'flex', flexDirection: 'column' }}>
         <Typography variant='caption' style={{ flexShrink: 0 }}>
@@ -73,45 +67,6 @@ export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props:
         </div>
     </div>);
 
-
-// TODO: move action creator to store directory
-export const loadProjectTreePickerProjects = (nodeId: string) =>
-    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId, pickerId: TreePickerId.PROJECTS }));
-
-        const ownerUuid = nodeId.length === 0 ? services.authService.getUuid() || '' : nodeId;
-
-        const filters = new FilterBuilder()
-            .addEqual('ownerUuid', ownerUuid)
-            .getFilters();
-
-        const { items } = await services.projectService.list({ filters });
-
-        dispatch<any>(receiveTreePickerData(nodeId, items, TreePickerId.PROJECTS));
-    };
-
-export const loadFavoriteTreePickerProjects = (nodeId: string) =>
-    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        const parentId = services.authService.getUuid() || '';
-
-        if (nodeId === '') {
-            dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId: parentId, pickerId: TreePickerId.FAVORITES }));
-            const { items } = await services.favoriteService.list(parentId);
-
-            dispatch<any>(receiveTreePickerData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
-        } else {
-            dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId, pickerId: TreePickerId.FAVORITES }));
-            const filters = new FilterBuilder()
-                .addEqual('ownerUuid', nodeId)
-                .getFilters();
-
-            const { items } = await services.projectService.list({ filters });
-
-            dispatch<any>(receiveTreePickerData(nodeId, items, TreePickerId.FAVORITES));
-        }
-
-    };
-
 const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
     switch (item.data.name) {
         case TreePickerId.FAVORITES:
@@ -128,23 +83,10 @@ const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
 const renderTreeItem = (item: TreeItem<ProjectResource>) =>
     <ListItemTextIcon
         icon={getProjectPickerIcon(item)}
-        name={item.data.name}
+        name={typeof item.data === 'string' ? item.data : item.data.name}
         isActive={item.active}
         hasMargin={true} />;
 
-
-// TODO: move action creator to store directory
-export const receiveTreePickerData = (nodeId: string, projects: ProjectResource[], pickerId: string) =>
-    (dispatch: Dispatch) => {
-        dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
-            nodeId,
-            nodes: projects.map(project => createTreePickerNode({ nodeId: project.uuid, value: project })),
-            pickerId,
-        }));
-
-        dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId }));
-    };
-
 export const ProjectTreePickerField = (props: WrappedFieldProps) =>
     <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
         <ProjectTreePicker onChange={handleChange(props)} />
