1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
8 FileArrayCommandInputParameter,
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, Divider, Grid, WithStyles, Typography } 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, getSelectedNodes, treePickerActions, getProjectsTreePickerIds } from '~/store/tree-picker/tree-picker-actions';
19 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
20 import { CollectionFile, CollectionFileType } from '~/models/collection-file';
21 import { createSelector, createStructuredSelector } from 'reselect';
22 import { ChipsInput } from '~/components/chips-input/chips-input';
23 import { identity, values, noop } from 'lodash';
24 import { InputProps } from '@material-ui/core/Input';
25 import { TreePicker } from '~/store/tree-picker/tree-picker';
26 import { RootState } from '~/store/store';
27 import { Chips } from '~/components/chips/chips';
28 import withStyles, { StyleRulesCallback } from '@material-ui/core/styles/withStyles';
30 export interface FileArrayInputProps {
31 input: FileArrayCommandInputParameter;
33 export const FileArrayInput = ({ input }: FileArrayInputProps) =>
37 component={FileArrayInputComponent}
40 validate={validationSelector(input)} />;
42 const parseFiles = (files: CollectionFile[] | string) =>
43 typeof files === 'string'
47 const parse = (file: CollectionFile): File => ({
50 location: `keep:${file.id}`,
54 const formatFiles = (files: File[] = []) =>
57 const format = (file: File): CollectionFile => ({
59 ? file.location.replace('keep:', '')
61 name: file.basename || '',
62 path: file.path || '',
64 type: CollectionFileType.FILE,
68 const validationSelector = createSelector(
70 isRequired => isRequired
75 const required = (value?: File[]) =>
76 value && value.length > 0
79 interface FileArrayInputComponentState {
81 files: CollectionFile[];
84 interface FileArrayInputComponentProps {
85 treePickerState: TreePicker;
88 const treePickerSelector = (state: RootState) => state.treePicker;
90 const mapStateToProps = createStructuredSelector({
91 treePickerState: treePickerSelector,
94 const FileArrayInputComponent = connect(mapStateToProps)(
95 class FileArrayInputComponent extends React.Component<FileArrayInputComponentProps & GenericInputProps & DispatchProp, FileArrayInputComponentState> {
96 state: FileArrayInputComponentState = {
101 fileRefreshTimeout = -1;
103 componentDidMount() {
104 this.props.dispatch<any>(
105 initProjectsTreePicker(this.props.commandInput.id));
116 this.setFilesFromProps(this.props.input.value);
117 this.setState({ open: true });
120 closeDialog = () => {
121 this.setState({ open: false });
126 this.props.input.onChange(this.state.files);
129 setFiles = (files: CollectionFile[]) => {
131 const deletedFiles = this.state.files
132 .reduce((deletedFiles, file) =>
133 files.some(({ id }) => id === file.id)
135 : [...deletedFiles, file]
138 this.setState({ files });
140 const ids = values(getProjectsTreePickerIds(this.props.commandInput.id));
141 ids.forEach(pickerId => {
143 treePickerActions.DESELECT_TREE_PICKER_NODE({
144 pickerId, id: deletedFiles.map(({ id }) => id),
151 setFilesFromProps = (files: CollectionFile[]) => {
153 const addedFiles = files
154 .reduce((addedFiles, file) =>
155 this.state.files.some(({ id }) => id === file.id)
157 : [...addedFiles, file]
160 const ids = values(getProjectsTreePickerIds(this.props.commandInput.id));
161 ids.forEach(pickerId => {
163 treePickerActions.SELECT_TREE_PICKER_NODE({
164 pickerId, id: addedFiles.map(({ id }) => id),
169 this.setFiles(files);
173 refreshFiles = () => {
174 clearTimeout(this.fileRefreshTimeout);
175 this.fileRefreshTimeout = setTimeout(this.setSelectedFiles);
178 setSelectedFiles = () => {
179 const nodes = getSelectedNodes<ProjectsTreePickerItem>(this.props.commandInput.id)(this.props.treePickerState);
180 const initialFiles: CollectionFile[] = [];
182 .reduce((files, { value }) =>
183 'type' in value && value.type === CollectionFileType.FILE
184 ? files.concat(value)
185 : files, initialFiles);
187 this.setFiles(files);
191 component={this.chipsInput}
196 value={this.props.input.value}
197 disabled={this.props.commandInput.disabled}
199 createNewValue={identity}
200 getLabel={(file: CollectionFile) => file.name}
201 inputComponent={this.textInput} />
203 textInput = (props: InputProps) =>
206 error={this.props.meta.touched && !!this.props.meta.error}
208 disabled={this.props.commandInput.disabled}
209 onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
210 onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined}
211 onBlur={this.props.input.onBlur} />
215 open={this.state.open}
216 onClose={this.closeDialog}
219 <DialogTitle>Choose files</DialogTitle>
221 <this.dialogContent />
224 <Button onClick={this.closeDialog}>Cancel</Button>
228 onClick={this.submit}>Ok</Button>
232 dialogContentStyles: StyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
235 flexDirection: 'column',
236 height: `${spacing.unit * 8}vh`,
243 margin: `${spacing.unit}px 0`,
248 padding: `${spacing.unit}px 0`,
253 dialogContent = withStyles(this.dialogContentStyles)(
254 ({ classes }: WithStyles<DialogContentCssRules>) =>
255 <div className={classes.root}>
256 <div className={classes.tree}>
258 pickerId={this.props.commandInput.id}
262 toggleItemSelection={this.refreshFiles} />
265 <div className={classes.chips}>
266 <Typography variant='subheading'>Selected files ({this.state.files.length}):</Typography>
270 values={this.state.files}
271 onChange={this.setFiles}
272 getLabel={(file: CollectionFile) => file.name} />
279 type DialogContentCssRules = 'root' | 'tree' | 'divider' | 'chips';