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 { 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';
21 type ProjectTreePickerProps = Pick<TreePickerProps<ProjectResource>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
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 }));
32 toggleItemOpen: (_, { id, status }, pickerId) => {
33 dispatch<any>(toggleItemOpen(id, status, pickerId));
35 toggleItemSelection: (_, { id }, pickerId) => {
36 dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id, pickerId }));
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));
48 // TODO: load sharedWithMe
51 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
55 const getNotSelectedTreePickerKind = (pickerId: string) => {
56 return [TreePickerId.PROJECTS, TreePickerId.FAVORITES, TreePickerId.SHARED_WITH_ME].filter(nodeId => nodeId !== pickerId);
59 export enum TreePickerId {
60 PROJECTS = 'Projects',
61 SHARED_WITH_ME = 'Shared with me',
62 FAVORITES = 'Favorites'
65 export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props: ProjectTreePickerProps) =>
66 <div style={{ display: 'flex', flexDirection: 'column' }}>
67 <Typography variant='caption' style={{ flexShrink: 0 }}>
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} />
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 }));
83 const ownerUuid = id.length === 0 ? services.authService.getUuid() || '' : id;
85 const filters = new FilterBuilder()
86 .addEqual('ownerUuid', ownerUuid)
89 const { items } = await services.projectService.list({ filters });
91 dispatch<any>(receiveTreePickerData(id, items, TreePickerId.PROJECTS));
94 export const loadFavoriteTreePickerProjects = (id: string) =>
95 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
96 const parentId = services.authService.getUuid() || '';
99 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.FAVORITES }));
100 const { items } = await services.favoriteService.list(parentId);
102 dispatch<any>(receiveTreePickerData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
104 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.FAVORITES }));
105 const filters = new FilterBuilder()
106 .addEqual('ownerUuid', id)
109 const { items } = await services.projectService.list({ filters });
111 dispatch<any>(receiveTreePickerData(id, items, TreePickerId.FAVORITES));
116 const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
117 switch (item.data.name) {
118 case TreePickerId.FAVORITES:
120 case TreePickerId.PROJECTS:
122 case TreePickerId.SHARED_WITH_ME:
129 const renderTreeItem = (item: TreeItem<ProjectResource>) =>
131 icon={getProjectPickerIcon(item)}
132 name={item.data.name}
133 isActive={item.active}
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({
142 nodes: projects.map(project => initTreeNode({ id: project.uuid, value: project })),
146 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
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'>
158 const handleChange = (props: WrappedFieldProps) => (value: string) =>
159 props.input.value === value
160 ? props.input.onChange('')
161 : props.input.onChange(value);