X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/533c057d79a87e7e48249d60fa5b45a9b4dd6823..798af50d33bc0f1e81e1743fc9832c6aa658be8e:/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 36104d43..215c31b7 100644
--- a/src/views-components/project-tree-picker/project-tree-picker.tsx
+++ b/src/views-components/project-tree-picker/project-tree-picker.tsx
@@ -9,14 +9,16 @@ 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, loadPublicFavoriteTreePickerProjects } 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 { ProjectIcon, FavoriteIcon, ProjectsIcon, ShareMeIcon, PublicFavoriteIcon } from '~/components/icon/icon';
 import { RootState } from "~/store/store";
 import { ServiceRepository } from "~/services/services";
-import { FilterBuilder } from "~/services/api/filter-builder";
 import { WrappedFieldProps } from 'redux-form';
-import { initTreeNode } from '~/models/tree';
+import { TreePickerId } from '~/models/tree';
+import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
+import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
+import { PickerIdProp } from '~/store/tree-picker/picker-id';
 
 type ProjectTreePickerProps = Pick<TreePickerProps<ProjectResource>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
 
@@ -44,7 +46,8 @@ const toggleItemOpen = (id: string, status: TreeItemStatus, pickerId: string) =>
                 dispatch<any>(loadProjectTreePickerProjects(id));
             } else if (pickerId === TreePickerId.FAVORITES) {
                 dispatch<any>(loadFavoriteTreePickerProjects(id === services.authService.getUuid() ? '' : id));
-            } else {
+            } else if (pickerId === TreePickerId.PUBLIC_FAVORITES) {
+                dispatch<any>(loadPublicFavoriteTreePickerProjects(id === services.authService.getUuid() ? '' : id));
                 // TODO: load sharedWithMe
             }
         } else {
@@ -56,12 +59,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 }}>
@@ -71,48 +68,10 @@ export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props:
             <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.PROJECTS} />
             <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.SHARED_WITH_ME} />
             <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.FAVORITES} />
+            <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.PUBLIC_FAVORITES} />
         </div>
     </div>);
 
-
-// TODO: move action creator to store directory
-export const loadProjectTreePickerProjects = (id: string) =>
-    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PROJECTS }));
-
-        const ownerUuid = id.length === 0 ? services.authService.getUuid() || '' : id;
-
-        const filters = new FilterBuilder()
-            .addEqual('ownerUuid', ownerUuid)
-            .getFilters();
-
-        const { items } = await services.projectService.list({ filters });
-
-        dispatch<any>(receiveTreePickerData(id, items, TreePickerId.PROJECTS));
-    };
-
-export const loadFavoriteTreePickerProjects = (id: string) =>
-    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        const parentId = services.authService.getUuid() || '';
-
-        if (id === '') {
-            dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: 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({ id, pickerId: TreePickerId.FAVORITES }));
-            const filters = new FilterBuilder()
-                .addEqual('ownerUuid', id)
-                .getFilters();
-
-            const { items } = await services.projectService.list({ filters });
-
-            dispatch<any>(receiveTreePickerData(id, items, TreePickerId.FAVORITES));
-        }
-
-    };
-
 const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
     switch (item.data.name) {
         case TreePickerId.FAVORITES:
@@ -121,6 +80,8 @@ const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
             return ProjectsIcon;
         case TreePickerId.SHARED_WITH_ME:
             return ShareMeIcon;
+        case TreePickerId.PUBLIC_FAVORITES:
+            return PublicFavoriteIcon;
         default:
             return ProjectIcon;
     }
@@ -129,34 +90,33 @@ 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 = (id: string, projects: ProjectResource[], pickerId: string) =>
-    (dispatch: Dispatch) => {
-        dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
-            id,
-            nodes: projects.map(project => initTreeNode({ id: project.uuid, value: project })),
-            pickerId,
-        }));
-
-        dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
-    };
-
-export const ProjectTreePickerField = (props: WrappedFieldProps) =>
+export const ProjectTreePickerField = (props: WrappedFieldProps & PickerIdProp) =>
     <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
-        <ProjectTreePicker onChange={handleChange(props)} />
+        <ProjectsTreePicker
+            pickerId={props.pickerId}
+            toggleItemActive={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);
+const handleChange = (props: WrappedFieldProps) =>
+    (_: any, { id }: TreeItem<ProjectsTreePickerItem>) =>
+        props.input.onChange(id);
 
+export const CollectionTreePickerField = (props: WrappedFieldProps & PickerIdProp) =>
+    <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
+        <ProjectsTreePicker
+            pickerId={props.pickerId}
+            toggleItemActive={handleChange(props)}
+            includeCollections />
+        {props.meta.dirty && props.meta.error &&
+            <Typography variant='caption' color='error'>
+                {props.meta.error}
+            </Typography>}
+    </div>;
\ No newline at end of file
