Move group details related actions to corresponding file
[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, loadProjectTreePickerProjects, loadFavoriteTreePickerProjects } 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 { WrappedFieldProps } from 'redux-form';
18 import { TreePickerId } from '~/models/tree';
19 import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
20 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
21 import { PickerIdProp } from '~/store/tree-picker/picker-id';
22
23 type ProjectTreePickerProps = Pick<TreePickerProps<ProjectResource>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
24
25 const mapDispatchToProps = (dispatch: Dispatch, props: { onChange: (projectUuid: string) => void }): ProjectTreePickerProps => ({
26     onContextMenu: () => { return; },
27     toggleItemActive: (_, { id }, pickerId) => {
28         getNotSelectedTreePickerKind(pickerId)
29             .forEach(pickerId => dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: '', pickerId })));
30         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId }));
31
32         props.onChange(id);
33     },
34     toggleItemOpen: (_, { id, status }, pickerId) => {
35         dispatch<any>(toggleItemOpen(id, status, pickerId));
36     },
37     toggleItemSelection: (_, { id }, pickerId) => {
38         dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id, pickerId }));
39     },
40 });
41
42 const toggleItemOpen = (id: string, status: TreeItemStatus, pickerId: string) =>
43     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44         if (status === TreeItemStatus.INITIAL) {
45             if (pickerId === TreePickerId.PROJECTS) {
46                 dispatch<any>(loadProjectTreePickerProjects(id));
47             } else if (pickerId === TreePickerId.FAVORITES) {
48                 dispatch<any>(loadFavoriteTreePickerProjects(id === services.authService.getUuid() ? '' : id));
49             } else {
50                 // TODO: load sharedWithMe
51             }
52         } else {
53             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
54         }
55     };
56
57 const getNotSelectedTreePickerKind = (pickerId: string) => {
58     return [TreePickerId.PROJECTS, TreePickerId.FAVORITES, TreePickerId.SHARED_WITH_ME].filter(nodeId => nodeId !== pickerId);
59 };
60
61 export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props: ProjectTreePickerProps) =>
62     <div style={{ display: 'flex', flexDirection: 'column' }}>
63         <Typography variant='caption' style={{ flexShrink: 0 }}>
64             Select a project
65         </Typography>
66         <div style={{ flexGrow: 1, overflow: 'auto' }}>
67             <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.PROJECTS} />
68             <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.SHARED_WITH_ME} />
69             <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.FAVORITES} />
70         </div>
71     </div>);
72
73 const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
74     switch (item.data.name) {
75         case TreePickerId.FAVORITES:
76             return FavoriteIcon;
77         case TreePickerId.PROJECTS:
78             return ProjectsIcon;
79         case TreePickerId.SHARED_WITH_ME:
80             return ShareMeIcon;
81         default:
82             return ProjectIcon;
83     }
84 };
85
86 const renderTreeItem = (item: TreeItem<ProjectResource>) =>
87     <ListItemTextIcon
88         icon={getProjectPickerIcon(item)}
89         name={typeof item.data === 'string' ? item.data : item.data.name}
90         isActive={item.active}
91         hasMargin={true} />;
92
93 export const ProjectTreePickerField = (props: WrappedFieldProps & PickerIdProp) =>
94     <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
95         <ProjectsTreePicker
96             pickerId={props.pickerId}
97             toggleItemActive={handleChange(props)} />
98         {props.meta.dirty && props.meta.error &&
99             <Typography variant='caption' color='error'>
100                 {props.meta.error}
101             </Typography>}
102     </div>;
103
104 const handleChange = (props: WrappedFieldProps) =>
105     (_: any, { id }: TreeItem<ProjectsTreePickerItem>) =>
106         props.input.onChange(id);