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 { CustomStyleRulesCallback } from 'common/custom-theme';
16 import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button, withStyles, WithStyles } from '@material-ui/core';
17 import { GenericInputProps, GenericInput } from './generic-input';
18 import { ProjectsTreePicker } from 'views-components/projects-tree-picker/projects-tree-picker';
19 import { connect, DispatchProp } from 'react-redux';
20 import { initProjectsTreePicker } from 'store/tree-picker/tree-picker-actions';
21 import { TreeItem } from 'components/tree/tree';
22 import { ProjectsTreePickerItem } from 'store/tree-picker/tree-picker-middleware';
23 import { CollectionFile, CollectionFileType } from 'models/collection-file';
25 export interface FileInputProps {
26 input: FileCommandInputParameter;
27 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
30 type DialogContentCssRules = 'root' | 'pickerWrapper';
32 export const FileInput = ({ input, options }: FileInputProps) =>
36 component={FileInputComponent as any}
42 validate={getValidation(input)} />;
44 const format = (value?: File) => value ? value.basename : '';
46 const parse = (file: CollectionFile): File => ({
48 location: `keep:${file.id}`,
52 const getValidation = memoize(
53 (input: FileCommandInputParameter) => ([
54 isRequiredInput(input)
55 ? (file?: File) => file ? undefined : ERROR_MESSAGE
59 interface FileInputComponentState {
61 file?: CollectionFile;
64 const FileInputComponent = connect()(
65 class FileInputComponent extends React.Component<GenericInputProps & DispatchProp & {
66 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
67 }, FileInputComponentState> {
68 state: FileInputComponentState = {
73 this.props.dispatch<any>(
74 initProjectsTreePicker(this.props.commandInput.id));
85 this.componentDidMount();
86 this.setState({ open: true });
90 this.setState({ open: false });
95 this.props.input.onChange(this.state.file);
98 setFile = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
99 if ('type' in data && data.type === CollectionFileType.FILE) {
100 this.setState({ file: data });
102 this.setState({ file: undefined });
112 disabled={props.commandInput.disabled}
113 value={props.input.value}
114 error={props.meta.touched && !!props.meta.error}
115 onClick={!props.commandInput.disabled ? this.openDialog : undefined}
116 onKeyPress={!props.commandInput.disabled ? this.openDialog : undefined} />}
120 dialogContentStyles: CustomStyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
123 flexDirection: 'column',
126 flexBasis: `${spacing(8)}vh`,
130 flexDirection: 'column',
134 dialog = withStyles(this.dialogContentStyles)(
135 ({ classes }: WithStyles<DialogContentCssRules>) =>
137 open={this.state.open}
138 onClose={this.closeDialog}
140 data-cy="choose-a-file-dialog"
142 <DialogTitle>Choose a file</DialogTitle>
143 <DialogContent className={classes.root}>
144 <div className={classes.pickerWrapper}>
146 pickerId={this.props.commandInput.id}
150 cascadeSelection={false}
151 options={this.props.options}
152 toggleItemActive={this.setFile} />
156 <Button onClick={this.closeDialog}>Cancel</Button>
158 disabled={!this.state.file}
161 onClick={this.submit}>Ok</Button>