1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import 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, StyleRulesCallback, withStyles, WithStyles } 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 'store/tree-picker/tree-picker-middleware';
22 import { CollectionFile, CollectionFileType } from 'models/collection-file';
24 export interface FileInputProps {
25 input: FileCommandInputParameter;
26 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
29 type DialogContentCssRules = 'root' | 'pickerWrapper';
31 export const FileInput = ({ input, options }: FileInputProps) =>
35 component={FileInputComponent as any}
41 validate={getValidation(input)} />;
43 const format = (value?: File) => value ? value.basename : '';
45 const parse = (file: CollectionFile): File => ({
47 location: `keep:${file.id}`,
51 const getValidation = memoize(
52 (input: FileCommandInputParameter) => ([
53 isRequiredInput(input)
54 ? (file?: File) => file ? undefined : ERROR_MESSAGE
58 interface FileInputComponentState {
60 file?: CollectionFile;
63 const FileInputComponent = connect()(
64 class FileInputComponent extends React.Component<GenericInputProps & DispatchProp & {
65 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
66 }, FileInputComponentState> {
67 state: FileInputComponentState = {
72 this.props.dispatch<any>(
73 initProjectsTreePicker(this.props.commandInput.id));
84 this.componentDidMount();
85 this.setState({ open: true });
89 this.setState({ open: false });
94 this.props.input.onChange(this.state.file);
97 setFile = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
98 if ('type' in data && data.type === CollectionFileType.FILE) {
99 this.setState({ file: data });
101 this.setState({ file: undefined });
111 disabled={props.commandInput.disabled}
112 value={props.input.value}
113 error={props.meta.touched && !!props.meta.error}
114 onClick={!props.commandInput.disabled ? this.openDialog : undefined}
115 onKeyPress={!props.commandInput.disabled ? this.openDialog : undefined} />}
119 dialogContentStyles: StyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
122 flexDirection: 'column',
125 flexBasis: `${spacing.unit * 8}vh`,
131 dialog = withStyles(this.dialogContentStyles)(
132 ({ classes }: WithStyles<DialogContentCssRules>) =>
134 open={this.state.open}
135 onClose={this.closeDialog}
137 data-cy="choose-a-file-dialog"
139 <DialogTitle>Choose a file</DialogTitle>
140 <DialogContent className={classes.root}>
141 <div className={classes.pickerWrapper}>
143 pickerId={this.props.commandInput.id}
147 cascadeSelection={false}
148 options={this.props.options}
149 toggleItemActive={this.setFile} />
153 <Button onClick={this.closeDialog}>Cancel</Button>
155 disabled={!this.state.file}
158 onClick={this.submit}>Ok</Button>