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;
26 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
28 export const FileInput = ({ input, options }: FileInputProps) =>
32 component={FileInputComponent}
38 validate={getValidation(input)} />;
40 const format = (value?: File) => value ? value.basename : '';
42 const parse = (file: CollectionFile): File => ({
44 location: `keep:${file.id}`,
48 const getValidation = memoize(
49 (input: FileCommandInputParameter) => ([
50 isRequiredInput(input)
51 ? (file?: File) => file ? undefined : ERROR_MESSAGE
55 interface FileInputComponentState {
57 file?: CollectionFile;
60 const FileInputComponent = connect()(
61 class FileInputComponent extends React.Component<GenericInputProps & DispatchProp & {
62 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
63 }, FileInputComponentState> {
64 state: FileInputComponentState = {
69 this.props.dispatch<any>(
70 initProjectsTreePicker(this.props.commandInput.id));
81 this.setState({ open: true });
85 this.setState({ open: false });
90 this.props.input.onChange(this.state.file);
93 setFile = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
94 if ('type' in data && data.type === CollectionFileType.FILE) {
95 this.setState({ file: data });
97 this.setState({ file: undefined });
107 disabled={props.commandInput.disabled}
108 value={props.input.value}
109 error={props.meta.touched && !!props.meta.error}
110 onClick={!props.commandInput.disabled ? this.openDialog : undefined}
111 onKeyPress={!props.commandInput.disabled ? this.openDialog : undefined} />}
117 open={this.state.open}
118 onClose={this.closeDialog}
120 data-cy="choose-a-file-dialog"
122 <DialogTitle>Choose a file</DialogTitle>
125 pickerId={this.props.commandInput.id}
128 options={this.props.options}
129 toggleItemActive={this.setFile} />
132 <Button onClick={this.closeDialog}>Cancel</Button>
134 disabled={!this.state.file}
137 onClick={this.submit}>Ok</Button>