1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
8 FileCommandInputParameter,
11 } from '~/models/workflow';
12 import { Field } from 'redux-form';
13 import { ERROR_MESSAGE } from '~/validators/require';
14 import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button } from '@material-ui/core';
15 import { GenericInputProps, GenericInput } from './generic-input';
16 import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
17 import { connect, DispatchProp } from 'react-redux';
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 { CollectionFile, CollectionFileType } from '~/models/collection-file';
23 export interface FileInputProps {
24 input: FileCommandInputParameter;
26 export const FileInput = ({ input }: FileInputProps) =>
30 component={FileInputComponent}
31 format={(value?: File) => value ? value.basename : ''}
32 parse={(file: CollectionFile): File => ({
34 location: `keep:${file.id}`,
38 isRequiredInput(input)
39 ? (file?: File) => file ? undefined : ERROR_MESSAGE
44 interface FileInputComponentState {
46 file?: CollectionFile;
49 const FileInputComponent = connect()(
50 class FileInputComponent extends React.Component<GenericInputProps & DispatchProp, FileInputComponentState> {
51 state: FileInputComponentState = {
56 this.props.dispatch<any>(
57 initProjectsTreePicker(this.props.commandInput.id));
68 this.setState({ open: true });
72 this.setState({ open: false });
77 this.props.input.onChange(this.state.file);
80 setFile = (event: React.MouseEvent<HTMLElement>, { data }: TreeItem<ProjectsTreePickerItem>, pickerId: string) => {
81 if ('type' in data && data.type === CollectionFileType.FILE) {
82 this.setState({ file: data });
84 this.setState({ file: undefined });
94 disabled={props.commandInput.disabled}
95 value={props.input.value}
96 error={props.meta.touched && !!props.meta.error}
97 onClick={!props.commandInput.disabled ? this.openDialog : undefined}
98 onKeyPress={!props.commandInput.disabled ? this.openDialog : undefined} />}
104 open={this.state.open}
105 onClose={this.closeDialog}
108 <DialogTitle>Choose a file</DialogTitle>
111 pickerId={this.props.commandInput.id}
114 toggleItemActive={this.setFile} />
117 <Button onClick={this.closeDialog}>Cancel</Button>
119 disabled={!this.state.file}
122 onClick={this.submit}>Ok</Button>