1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { memoize } from 'lodash/fp';
9 FileCommandInputParameter,
12 } from '~/models/workflow';
13 import { Field } from 'redux-form';
14 import { ERROR_MESSAGE } from '~/validators/require';
15 import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button } from '@material-ui/core';
16 import { GenericInputProps, GenericInput } from './generic-input';
17 import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
18 import { connect, DispatchProp } from 'react-redux';
19 import { initProjectsTreePicker } from '~/store/tree-picker/tree-picker-actions';
20 import { TreeItem } from '~/components/tree/tree';
21 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
22 import { CollectionFile, CollectionFileType } from '~/models/collection-file';
24 export interface FileInputProps {
25 input: FileCommandInputParameter;
27 export const FileInput = ({ input }: FileInputProps) =>
31 component={FileInputComponent}
34 validate={getValidation(input)} />;
36 const format = (value?: File) => value ? value.basename : '';
38 const parse = (file: CollectionFile): File => ({
40 location: `keep:${file.id}`,
44 const getValidation = memoize(
45 (input: FileCommandInputParameter) => ([
46 isRequiredInput(input)
47 ? (file?: File) => file ? undefined : ERROR_MESSAGE
51 interface FileInputComponentState {
53 file?: CollectionFile;
56 const FileInputComponent = connect()(
57 class FileInputComponent extends React.Component<GenericInputProps & DispatchProp, FileInputComponentState> {
58 state: FileInputComponentState = {
63 this.props.dispatch<any>(
64 initProjectsTreePicker(this.props.commandInput.id));
75 this.setState({ open: true });
79 this.setState({ open: false });
84 this.props.input.onChange(this.state.file);
87 setFile = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
88 if ('type' in data && data.type === CollectionFileType.FILE) {
89 this.setState({ file: data });
91 this.setState({ file: undefined });
101 disabled={props.commandInput.disabled}
102 value={props.input.value}
103 error={props.meta.touched && !!props.meta.error}
104 onClick={!props.commandInput.disabled ? this.openDialog : undefined}
105 onKeyPress={!props.commandInput.disabled ? this.openDialog : undefined} />}
111 open={this.state.open}
112 onClose={this.closeDialog}
115 <DialogTitle>Choose a file</DialogTitle>
118 pickerId={this.props.commandInput.id}
121 toggleItemActive={this.setFile} />
124 <Button onClick={this.closeDialog}>Cancel</Button>
126 disabled={!this.state.file}
129 onClick={this.submit}>Ok</Button>