1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { connect, DispatchProp } from 'react-redux';
7 import { memoize } from 'lodash/fp';
8 import { Field } from 'redux-form';
9 import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button } from '@material-ui/core';
12 DirectoryCommandInputParameter,
15 } from '~/models/workflow';
16 import { GenericInputProps, GenericInput } from './generic-input';
17 import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
18 import { initProjectsTreePicker } from '~/store/tree-picker/tree-picker-actions';
19 import { TreeItem } from '~/components/tree/tree';
20 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
21 import { CollectionResource } from '~/models/collection';
22 import { ResourceKind } from '~/models/resource';
23 import { ERROR_MESSAGE } from '~/validators/require';
25 export interface DirectoryInputProps {
26 input: DirectoryCommandInputParameter;
28 export const DirectoryInput = ({ input }: DirectoryInputProps) =>
32 component={DirectoryInputComponent}
35 validate={getValidation(input)} />;
37 const format = (value?: Directory) => value ? value.basename : '';
39 const parse = (directory: CollectionResource): Directory => ({
40 class: CWLType.DIRECTORY,
41 location: `keep:${directory.portableDataHash}`,
42 basename: directory.name,
45 const getValidation = memoize(
46 (input: DirectoryCommandInputParameter) => ([
47 isRequiredInput(input)
48 ? (directory?: Directory) => directory ? undefined : ERROR_MESSAGE
53 interface DirectoryInputComponentState {
55 directory?: CollectionResource;
58 const DirectoryInputComponent = connect()(
59 class FileInputComponent extends React.Component<GenericInputProps & DispatchProp, DirectoryInputComponentState> {
60 state: DirectoryInputComponentState = {
65 this.props.dispatch<any>(
66 initProjectsTreePicker(this.props.commandInput.id));
77 this.setState({ open: true });
81 this.setState({ open: false });
86 this.props.input.onChange(this.state.directory);
89 setDirectory = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
90 if ('kind' in data && data.kind === ResourceKind.COLLECTION) {
91 this.setState({ directory: data });
93 this.setState({ directory: undefined });
103 value={props.input.value}
104 error={props.meta.touched && !!props.meta.error}
105 disabled={props.commandInput.disabled}
106 onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
107 onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined} />}
113 open={this.state.open}
114 onClose={this.closeDialog}
117 <DialogTitle>Choose a directory</DialogTitle>
120 pickerId={this.props.commandInput.id}
122 toggleItemActive={this.setDirectory} />
125 <Button onClick={this.closeDialog}>Cancel</Button>
127 disabled={!this.state.directory}
130 onClick={this.submit}>Ok</Button>