Merge branch '21703-collection-update-lock'
[arvados.git] / services / workbench2 / src / views-components / projects-tree-picker / tree-picker-field.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from "react";
6 import { Typography } from "@material-ui/core";
7 import { TreeItem } from "components/tree/tree";
8 import { WrappedFieldProps } from 'redux-form';
9 import { ProjectsTreePicker } from 'views-components/projects-tree-picker/projects-tree-picker';
10 import { ProjectsTreePickerItem } from 'store/tree-picker/tree-picker-middleware';
11 import { PickerIdProp } from 'store/tree-picker/picker-id';
12 import { FileOperationLocation, getFileOperationLocation } from "store/tree-picker/tree-picker-actions";
13 import { connect } from "react-redux";
14 import { Dispatch } from "redux";
15
16 export const ProjectTreePickerField = (props: WrappedFieldProps & PickerIdProp) =>
17     <div style={{ display: 'flex', minHeight: 0, flexDirection: 'column' }}>
18         <div style={{ flexBasis: '960px', flexShrink: 1, minHeight: 0, display: 'flex', flexDirection: 'column' }}>
19             <ProjectsTreePicker
20                 pickerId={props.pickerId}
21                 toggleItemActive={handleChange(props)}
22                 cascadeSelection={false}
23                 options={{ showOnlyOwned: false, showOnlyWritable: true }} />
24             {props.meta.dirty && props.meta.error &&
25                 <Typography variant='caption' color='error'>
26                     {props.meta.error}
27                 </Typography>}
28         </div>
29     </div>;
30
31 const handleChange = (props: WrappedFieldProps) =>
32     (_: any, { id }: TreeItem<ProjectsTreePickerItem>) =>
33         props.input.onChange(id);
34
35 export const CollectionTreePickerField = (props: WrappedFieldProps & PickerIdProp) =>
36     <div style={{ display: 'flex', minHeight: 0, flexDirection: 'column' }}>
37         <div style={{ flexBasis: '275px', flexShrink: 1, minHeight: 0, display: 'flex', flexDirection: 'column' }}>
38             <ProjectsTreePicker
39                 pickerId={props.pickerId}
40                 toggleItemActive={handleChange(props)}
41                 cascadeSelection={false}
42                 options={{ showOnlyOwned: false, showOnlyWritable: true }}
43                 includeCollections />
44             {props.meta.dirty && props.meta.error &&
45                 <Typography variant='caption' color='error'>
46                     {props.meta.error}
47                 </Typography>}
48         </div>
49     </div>;
50
51 type ProjectsTreePickerActionProps = {
52     getFileOperationLocation: (item: ProjectsTreePickerItem) => Promise<FileOperationLocation | undefined>;
53 }
54
55 const projectsTreePickerMapDispatchToProps = (dispatch: Dispatch): ProjectsTreePickerActionProps => ({
56     getFileOperationLocation: (item: ProjectsTreePickerItem) => dispatch<any>(getFileOperationLocation(item)),
57 });
58
59 type ProjectsTreePickerCombinedProps = ProjectsTreePickerActionProps & WrappedFieldProps & PickerIdProp;
60
61 export const DirectoryTreePickerField = connect(null, projectsTreePickerMapDispatchToProps)(
62     class DirectoryTreePickerFieldComponent extends React.Component<ProjectsTreePickerCombinedProps> {
63
64         handleDirectoryChange = (props: WrappedFieldProps) =>
65             async (_: any, { data }: TreeItem<ProjectsTreePickerItem>) => {
66                 const location = await this.props.getFileOperationLocation(data);
67                 props.input.onChange(location || '');
68             }
69
70         render() {
71             return <div style={{ display: 'flex', minHeight: 0, flexDirection: 'column' }}>
72                 <div style={{ flexBasis: '275px', flexShrink: 1, minHeight: 0, display: 'flex', flexDirection: 'column' }}>
73                     <ProjectsTreePicker
74                         currentUuids={[this.props.input.value.uuid]}
75                         pickerId={this.props.pickerId}
76                         toggleItemActive={this.handleDirectoryChange(this.props)}
77                         cascadeSelection={false}
78                         options={{ showOnlyOwned: false, showOnlyWritable: true }}
79                         includeCollections
80                         includeDirectories />
81                     {this.props.meta.dirty && this.props.meta.error &&
82                         <Typography variant='caption' color='error'>
83                             {this.props.meta.error}
84                         </Typography>}
85                 </div>
86             </div>;
87         }
88     });