1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
23 type ProjectTreePickerProps = Pick<TreePickerProps<ProjectResource>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
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 }));
34 toggleItemOpen: (_, { id, status }, pickerId) => {
35 dispatch<any>(toggleItemOpen(id, status, pickerId));
37 toggleItemSelection: (_, { id }, pickerId) => {
38 dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id, pickerId }));
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));
50 // TODO: load sharedWithMe
53 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
57 const getNotSelectedTreePickerKind = (pickerId: string) => {
58 return [TreePickerId.PROJECTS, TreePickerId.FAVORITES, TreePickerId.SHARED_WITH_ME].filter(nodeId => nodeId !== pickerId);
61 export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props: ProjectTreePickerProps) =>
62 <div style={{ display: 'flex', flexDirection: 'column' }}>
63 <Typography variant='caption' style={{ flexShrink: 0 }}>
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} />
73 const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
74 switch (item.data.name) {
75 case TreePickerId.FAVORITES:
77 case TreePickerId.PROJECTS:
79 case TreePickerId.SHARED_WITH_ME:
86 const renderTreeItem = (item: TreeItem<ProjectResource>) =>
88 icon={getProjectPickerIcon(item)}
89 name={typeof item.data === 'string' ? item.data : item.data.name}
90 isActive={item.active}
93 export const ProjectTreePickerField = (props: WrappedFieldProps & PickerIdProp) =>
94 <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
96 pickerId={props.pickerId}
97 toggleItemActive={handleChange(props)} />
98 {props.meta.dirty && props.meta.error &&
99 <Typography variant='caption' color='error'>
104 const handleChange = (props: WrappedFieldProps) =>
105 (_: any, { id }: TreeItem<ProjectsTreePickerItem>) =>
106 props.input.onChange(id);