X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f2b031de2183439f8aade2f290cd0e3f95f6438c..c175f990704fdef165616125433e2079a00a3437:/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 ddf684381a..3859180f0e 100644
--- a/src/views-components/project-tree-picker/project-tree-picker.tsx
+++ b/src/views-components/project-tree-picker/project-tree-picker.tsx
@@ -16,43 +16,44 @@ import { createTreePickerNode } from "~/store/tree-picker/tree-picker";
 import { RootState } from "~/store/store";
 import { ServiceRepository } from "~/services/services";
 import { FilterBuilder } from "~/common/api/filter-builder";
-import { mockProjectResource } from "~/models/test-utils";
+import { WrappedFieldProps } from 'redux-form';
 
-type ProjectTreePickerProps = Pick<TreePickerProps, 'toggleItemActive' | 'toggleItemOpen'>;
+type ProjectTreePickerProps = Pick<TreePickerProps, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen'>;
 
 const mapDispatchToProps = (dispatch: Dispatch, props: { onChange: (projectUuid: string) => void }): ProjectTreePickerProps => ({
-    toggleItemActive: (id, status, pickerKind) => {
-        getNotSelectedTreePickerKind(pickerKind)
-            .forEach(pickerKind => dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ id: '', pickerKind })));
-        dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ id, pickerKind }));
+    onContextMenu: () => { return; },
+    toggleItemActive: (nodeId, status, pickerId) => {
+        getNotSelectedTreePickerKind(pickerId)
+            .forEach(pickerId => dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId: '', pickerId })));
+        dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId, pickerId }));
 
-        props.onChange(id);
+        props.onChange(nodeId);
     },
-    toggleItemOpen: (id, status, pickerKind) => {
-        dispatch<any>(toggleItemOpen(id, status, pickerKind));
+    toggleItemOpen: (nodeId, status, pickerId) => {
+        dispatch<any>(toggleItemOpen(nodeId, status, pickerId));
     }
 });
 
-const toggleItemOpen = (id: string, status: TreeItemStatus, pickerKind: string) =>
+const toggleItemOpen = (nodeId: string, status: TreeItemStatus, pickerId: string) =>
     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         if (status === TreeItemStatus.INITIAL) {
-            if (pickerKind === TreePickerKind.PROJECTS) {
-                dispatch<any>(loadProjectTreePickerProjects(id));
-            } else if (pickerKind === TreePickerKind.FAVORITES) {
-                dispatch<any>(loadFavoriteTreePickerProjects(id === services.authService.getUuid() ? '' : id));
+            if (pickerId === TreePickerId.PROJECTS) {
+                dispatch<any>(loadProjectTreePickerProjects(nodeId));
+            } else if (pickerId === TreePickerId.FAVORITES) {
+                dispatch<any>(loadFavoriteTreePickerProjects(nodeId === services.authService.getUuid() ? '' : nodeId));
             } else {
                 // TODO: load sharedWithMe
             }
         } else {
-            dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerKind }));
+            dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId }));
         }
     };
 
-const getNotSelectedTreePickerKind = (pickerKind: string) => {
-    return [TreePickerKind.PROJECTS, TreePickerKind.FAVORITES, TreePickerKind.SHARED_WITH_ME].filter(id => id !== pickerKind);
+const getNotSelectedTreePickerKind = (pickerId: string) => {
+    return [TreePickerId.PROJECTS, TreePickerId.FAVORITES, TreePickerId.SHARED_WITH_ME].filter(nodeId => nodeId !== pickerId);
 };
 
-export enum TreePickerKind {
+export enum TreePickerId {
     PROJECTS = 'Projects',
     SHARED_WITH_ME = 'Shared with me',
     FAVORITES = 'Favorites'
@@ -64,19 +65,19 @@ export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props:
             Select a project
         </Typography>
         <div style={{ flexGrow: 1, overflow: 'auto' }}>
-            <TreePicker {...props} render={renderTreeItem} pickerKind={TreePickerKind.PROJECTS} />
-            <TreePicker {...props} render={renderTreeItem} pickerKind={TreePickerKind.SHARED_WITH_ME} />
-            <TreePicker {...props} render={renderTreeItem} pickerKind={TreePickerKind.FAVORITES} />
+            <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.PROJECTS} />
+            <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.SHARED_WITH_ME} />
+            <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.FAVORITES} />
         </div>
     </div>);
 
 
 // TODO: move action creator to store directory
-export const loadProjectTreePickerProjects = (id: string) =>
+export const loadProjectTreePickerProjects = (nodeId: string) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerKind: TreePickerKind.PROJECTS }));
+        dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId, pickerId: TreePickerId.PROJECTS }));
 
-        const ownerUuid = id.length === 0 ? services.authService.getUuid() || '' : id;
+        const ownerUuid = nodeId.length === 0 ? services.authService.getUuid() || '' : nodeId;
 
         const filters = new FilterBuilder()
             .addEqual('ownerUuid', ownerUuid)
@@ -84,38 +85,38 @@ export const loadProjectTreePickerProjects = (id: string) =>
 
         const { items } = await services.projectService.list({ filters });
 
-        dispatch<any>(receiveTreePickerData(id, items, TreePickerKind.PROJECTS));
+        dispatch<any>(receiveTreePickerData(nodeId, items, TreePickerId.PROJECTS));
     };
 
-export const loadFavoriteTreePickerProjects = (id: string) =>
+export const loadFavoriteTreePickerProjects = (nodeId: string) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         const parentId = services.authService.getUuid() || '';
 
-        if (id === '') {
-            dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerKind: TreePickerKind.FAVORITES }));
+        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[], TreePickerKind.FAVORITES));
+            dispatch<any>(receiveTreePickerData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
         } else {
-            dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerKind: TreePickerKind.FAVORITES }));
+            dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId, pickerId: TreePickerId.FAVORITES }));
             const filters = new FilterBuilder()
-                .addEqual('ownerUuid', id)
+                .addEqual('ownerUuid', nodeId)
                 .getFilters();
 
             const { items } = await services.projectService.list({ filters });
 
-            dispatch<any>(receiveTreePickerData(id, items, TreePickerKind.FAVORITES));
+            dispatch<any>(receiveTreePickerData(nodeId, items, TreePickerId.FAVORITES));
         }
 
     };
 
 const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
     switch (item.data.name) {
-        case TreePickerKind.FAVORITES:
+        case TreePickerId.FAVORITES:
             return FavoriteIcon;
-        case TreePickerKind.PROJECTS:
+        case TreePickerId.PROJECTS:
             return ProjectsIcon;
-        case TreePickerKind.SHARED_WITH_ME:
+        case TreePickerId.SHARED_WITH_ME:
             return ShareMeIcon;
         default:
             return ProjectIcon;
@@ -131,42 +132,28 @@ const renderTreeItem = (item: TreeItem<ProjectResource>) =>
 
 
 // TODO: move action creator to store directory
-export const receiveTreePickerData = (id: string, projects: ProjectResource[], pickerKind: string) =>
+export const receiveTreePickerData = (nodeId: string, projects: ProjectResource[], pickerId: string) =>
     (dispatch: Dispatch) => {
         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
-            id,
-            nodes: projects.map(project => createTreePickerNode({ id: project.uuid, value: project })),
-            pickerKind,
+            nodeId,
+            nodes: projects.map(project => createTreePickerNode({ nodeId: project.uuid, value: project })),
+            pickerId,
         }));
 
-        dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerKind }));
+        dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId }));
     };
 
-export const initPickerProjectTree = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-    const uuid = services.authService.getUuid();
-
-    dispatch<any>(getPickerTreeProjects(uuid));
-    dispatch<any>(getSharedWithMeProjectsPickerTree(uuid));
-    dispatch<any>(getFavoritesProjectsPickerTree(uuid));
-};
-
-const getPickerTreeProjects = (uuid: string = '') => {
-    return getProjectsPickerTree(uuid, TreePickerKind.PROJECTS);
-};
-
-const getSharedWithMeProjectsPickerTree = (uuid: string = '') => {
-    return getProjectsPickerTree(uuid, TreePickerKind.SHARED_WITH_ME);
-};
-
-const getFavoritesProjectsPickerTree = (uuid: string = '') => {
-    return getProjectsPickerTree(uuid, TreePickerKind.FAVORITES);
-};
-
-const getProjectsPickerTree = (uuid: string, kind: string) => {
-    return receiveTreePickerData(
-        '',
-        [mockProjectResource({ uuid, name: kind })],
-        kind
-    );
-};
+export const ProjectTreePickerField = (props: WrappedFieldProps) =>
+    <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
+        <ProjectTreePicker onChange={handleChange(props)} />
+        {props.meta.dirty && props.meta.error &&
+            <Typography variant='caption' color='error'>
+                {props.meta.error}
+            </Typography>}
+    </div>;
+
+const handleChange = (props: WrappedFieldProps) => (value: string) =>
+    props.input.value === value
+        ? props.input.onChange('')
+        : props.input.onChange(value);
 
