metadata-without-navigation
[arvados.git] / src / views-components / project-tree-picker / project-tree-picker.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from "react";
6 import { Dispatch } from "redux";
7 import { connect } from "react-redux";
8 import { Typography } from "@material-ui/core";
9 import { TreePicker, TreePickerProps } from "../tree-picker/tree-picker";
10 import { TreeItem, TreeItemStatus } from "~/components/tree/tree";
11 import { ProjectResource } from "~/models/project";
12 import { treePickerActions } from "~/store/tree-picker/tree-picker-actions";
13 import { ListItemTextIcon } from "~/components/list-item-text-icon/list-item-text-icon";
14 import { ProjectIcon, FavoriteIcon, ProjectsIcon, ShareMeIcon } from "~/components/icon/icon";
15 import { RootState } from "~/store/store";
16 import { ServiceRepository } from "~/services/services";
17 import { FilterBuilder } from "~/services/api/filter-builder";
18 import { WrappedFieldProps } from 'redux-form';
19 import { initTreeNode } from '~/models/tree';
20
21 type ProjectTreePickerProps = Pick<TreePickerProps<ProjectResource>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
22
23 const mapDispatchToProps = (dispatch: Dispatch, props: { onChange: (projectUuid: string) => void }): ProjectTreePickerProps => ({
24     onContextMenu: () => { return; },
25     toggleItemActive: (_, { id }, pickerId) => {
26         getNotSelectedTreePickerKind(pickerId)
27             .forEach(pickerId => dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: '', pickerId })));
28         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId }));
29
30         props.onChange(id);
31     },
32     toggleItemOpen: (_, { id, status }, pickerId) => {
33         dispatch<any>(toggleItemOpen(id, status, pickerId));
34     },
35     toggleItemSelection: (_, { id }, pickerId) => {
36         dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id, pickerId }));
37     },
38 });
39
40 const toggleItemOpen = (id: string, status: TreeItemStatus, pickerId: string) =>
41     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
42         if (status === TreeItemStatus.INITIAL) {
43             if (pickerId === TreePickerId.PROJECTS) {
44                 dispatch<any>(loadProjectTreePickerProjects(id));
45             } else if (pickerId === TreePickerId.FAVORITES) {
46                 dispatch<any>(loadFavoriteTreePickerProjects(id === services.authService.getUuid() ? '' : id));
47             } else {
48                 // TODO: load sharedWithMe
49             }
50         } else {
51             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
52         }
53     };
54
55 const getNotSelectedTreePickerKind = (pickerId: string) => {
56     return [TreePickerId.PROJECTS, TreePickerId.FAVORITES, TreePickerId.SHARED_WITH_ME].filter(nodeId => nodeId !== pickerId);
57 };
58
59 export enum TreePickerId {
60     PROJECTS = 'Projects',
61     SHARED_WITH_ME = 'Shared with me',
62     FAVORITES = 'Favorites'
63 }
64
65 export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props: ProjectTreePickerProps) =>
66     <div style={{ display: 'flex', flexDirection: 'column' }}>
67         <Typography variant='caption' style={{ flexShrink: 0 }}>
68             Select a project
69         </Typography>
70         <div style={{ flexGrow: 1, overflow: 'auto' }}>
71             <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.PROJECTS} />
72             <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.SHARED_WITH_ME} />
73             <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.FAVORITES} />
74         </div>
75     </div>);
76
77
78 // TODO: move action creator to store directory
79 export const loadProjectTreePickerProjects = (id: string) =>
80     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
81         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PROJECTS }));
82
83         const ownerUuid = id.length === 0 ? services.authService.getUuid() || '' : id;
84
85         const filters = new FilterBuilder()
86             .addEqual('ownerUuid', ownerUuid)
87             .getFilters();
88
89         const { items } = await services.projectService.list({ filters });
90
91         dispatch<any>(receiveTreePickerData(id, items, TreePickerId.PROJECTS));
92     };
93
94 export const loadFavoriteTreePickerProjects = (id: string) =>
95     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
96         const parentId = services.authService.getUuid() || '';
97
98         if (id === '') {
99             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.FAVORITES }));
100             const { items } = await services.favoriteService.list(parentId);
101
102             dispatch<any>(receiveTreePickerData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
103         } else {
104             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.FAVORITES }));
105             const filters = new FilterBuilder()
106                 .addEqual('ownerUuid', id)
107                 .getFilters();
108
109             const { items } = await services.projectService.list({ filters });
110
111             dispatch<any>(receiveTreePickerData(id, items, TreePickerId.FAVORITES));
112         }
113
114     };
115
116 const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
117     switch (item.data.name) {
118         case TreePickerId.FAVORITES:
119             return FavoriteIcon;
120         case TreePickerId.PROJECTS:
121             return ProjectsIcon;
122         case TreePickerId.SHARED_WITH_ME:
123             return ShareMeIcon;
124         default:
125             return ProjectIcon;
126     }
127 };
128
129 const renderTreeItem = (item: TreeItem<ProjectResource>) =>
130     <ListItemTextIcon
131         icon={getProjectPickerIcon(item)}
132         name={item.data.name}
133         isActive={item.active}
134         hasMargin={true} />;
135
136
137 // TODO: move action creator to store directory
138 export const receiveTreePickerData = (id: string, projects: ProjectResource[], pickerId: string) =>
139     (dispatch: Dispatch) => {
140         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
141             id,
142             nodes: projects.map(project => initTreeNode({ id: project.uuid, value: project })),
143             pickerId,
144         }));
145
146         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
147     };
148
149 export const ProjectTreePickerField = (props: WrappedFieldProps) =>
150     <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
151         <ProjectTreePicker onChange={handleChange(props)} />
152         {props.meta.dirty && props.meta.error &&
153             <Typography variant='caption' color='error'>
154                 {props.meta.error}
155             </Typography>}
156     </div>;
157
158 const handleChange = (props: WrappedFieldProps) => (value: string) =>
159     props.input.value === value
160         ? props.input.onChange('')
161         : props.input.onChange(value);
162