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 } 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 { createTreePickerNode } from "~/store/tree-picker/tree-picker";
16 import { RootState } from "~/store/store";
17 import { ServiceRepository } from "~/services/services";
18 import { FilterBuilder } from "~/common/api/filter-builder";
19 import { WrappedFieldProps } from 'redux-form';
21 type ProjectTreePickerProps = Pick<TreePickerProps, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen'>;
23 const mapDispatchToProps = (dispatch: Dispatch, props: { onChange: (projectUuid: string) => void }): ProjectTreePickerProps => ({
24 onContextMenu: () => { return; },
25 toggleItemActive: (nodeId, status, pickerId) => {
26 getNotSelectedTreePickerKind(pickerId)
27 .forEach(pickerId => dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId: '', pickerId })));
28 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId, pickerId }));
30 props.onChange(nodeId);
32 toggleItemOpen: (nodeId, status, pickerId) => {
33 dispatch<any>(toggleItemOpen(nodeId, status, pickerId));
37 const toggleItemOpen = (nodeId: string, status: TreeItemStatus, pickerId: string) =>
38 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39 if (status === TreeItemStatus.INITIAL) {
40 if (pickerId === TreePickerId.PROJECTS) {
41 dispatch<any>(loadProjectTreePickerProjects(nodeId));
42 } else if (pickerId === TreePickerId.FAVORITES) {
43 dispatch<any>(loadFavoriteTreePickerProjects(nodeId === services.authService.getUuid() ? '' : nodeId));
45 // TODO: load sharedWithMe
48 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId }));
52 const getNotSelectedTreePickerKind = (pickerId: string) => {
53 return [TreePickerId.PROJECTS, TreePickerId.FAVORITES, TreePickerId.SHARED_WITH_ME].filter(nodeId => nodeId !== pickerId);
56 export enum TreePickerId {
57 PROJECTS = 'Projects',
58 SHARED_WITH_ME = 'Shared with me',
59 FAVORITES = 'Favorites'
62 export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props: ProjectTreePickerProps) =>
63 <div style={{ display: 'flex', flexDirection: 'column' }}>
64 <Typography variant='caption' style={{ flexShrink: 0 }}>
67 <div style={{ flexGrow: 1, overflow: 'auto' }}>
68 <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.PROJECTS} />
69 <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.SHARED_WITH_ME} />
70 <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.FAVORITES} />
75 // TODO: move action creator to store directory
76 export const loadProjectTreePickerProjects = (nodeId: string) =>
77 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
78 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId, pickerId: TreePickerId.PROJECTS }));
80 const ownerUuid = nodeId.length === 0 ? services.authService.getUuid() || '' : nodeId;
82 const filters = new FilterBuilder()
83 .addEqual('ownerUuid', ownerUuid)
86 const { items } = await services.projectService.list({ filters });
88 dispatch<any>(receiveTreePickerData(nodeId, items, TreePickerId.PROJECTS));
91 export const loadFavoriteTreePickerProjects = (nodeId: string) =>
92 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
93 const parentId = services.authService.getUuid() || '';
96 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId: parentId, pickerId: TreePickerId.FAVORITES }));
97 const { items } = await services.favoriteService.list(parentId);
99 dispatch<any>(receiveTreePickerData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
101 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId, pickerId: TreePickerId.FAVORITES }));
102 const filters = new FilterBuilder()
103 .addEqual('ownerUuid', nodeId)
106 const { items } = await services.projectService.list({ filters });
108 dispatch<any>(receiveTreePickerData(nodeId, items, TreePickerId.FAVORITES));
113 const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
114 switch (item.data.name) {
115 case TreePickerId.FAVORITES:
117 case TreePickerId.PROJECTS:
119 case TreePickerId.SHARED_WITH_ME:
126 const renderTreeItem = (item: TreeItem<ProjectResource>) =>
128 icon={getProjectPickerIcon(item)}
129 name={item.data.name}
130 isActive={item.active}
134 // TODO: move action creator to store directory
135 export const receiveTreePickerData = (nodeId: string, projects: ProjectResource[], pickerId: string) =>
136 (dispatch: Dispatch) => {
137 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
139 nodes: projects.map(project => createTreePickerNode({ nodeId: project.uuid, value: project })),
143 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId }));
146 export const ProjectTreePickerField = (props: WrappedFieldProps) =>
147 <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
148 <ProjectTreePicker onChange={handleChange(props)} />
149 {props.meta.dirty && props.meta.error &&
150 <Typography variant='caption' color='error'>
155 const handleChange = (props: WrappedFieldProps) => (value: string) =>
156 props.input.value === value
157 ? props.input.onChange('')
158 : props.input.onChange(value);