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 });
121 closeDialog = () => {
122 this.setState({ open: false });
127 this.props.input.onChange(this.state.files);
130 setFiles = (files: CollectionFile[]) => {
132 const deletedFiles = this.state.files
133 .reduce((deletedFiles, file) =>
134 files.some(({ id }) => id === file.id)
136 : [...deletedFiles, file]
139 this.setState({ files });
141 const ids = values(getProjectsTreePickerIds(this.props.commandInput.id));
142 ids.forEach(pickerId => {
144 treePickerActions.DESELECT_TREE_PICKER_NODE({
145 pickerId, id: deletedFiles.map(({ id }) => id),
152 setFilesFromProps = (files: CollectionFile[]) => {
154 const addedFiles = files
155 .reduce((addedFiles, file) =>
156 this.state.files.some(({ id }) => id === file.id)
158 : [...addedFiles, file]
161 const ids = values(getProjectsTreePickerIds(this.props.commandInput.id));
162 ids.forEach(pickerId => {
164 treePickerActions.SELECT_TREE_PICKER_NODE({
165 pickerId, id: addedFiles.map(({ id }) => id),
170 this.setFiles(files);
174 refreshFiles = () => {
175 clearTimeout(this.fileRefreshTimeout);
176 this.fileRefreshTimeout = setTimeout(this.setSelectedFiles);
179 setSelectedFiles = () => {
180 const nodes = getSelectedNodes<ProjectsTreePickerItem>(this.props.commandInput.id)(this.props.treePickerState);
181 const initialFiles: CollectionFile[] = [];
183 .reduce((files, { value }) =>
184 'type' in value && value.type === CollectionFileType.FILE
185 ? files.concat(value)
186 : files, initialFiles);
188 this.setFiles(files);
192 component={this.chipsInput}
197 value={this.props.input.value}
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 onClick={this.openDialog}
209 onKeyPress={this.openDialog}
210 onBlur={this.props.input.onBlur} />
214 open={this.state.open}
215 onClose={this.closeDialog}
218 <DialogTitle>Choose files</DialogTitle>
220 <this.dialogContent />
223 <Button onClick={this.closeDialog}>Cancel</Button>
227 onClick={this.submit}>Ok</Button>
231 dialogContentStyles: StyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
234 flexDirection: 'column',
235 height: `${spacing.unit * 8}vh`,
242 margin: `${spacing.unit}px 0`,
247 padding: `${spacing.unit}px 0`,
252 dialogContent = withStyles(this.dialogContentStyles)(
253 ({ classes }: WithStyles<DialogContentCssRules>) =>
254 <div className={classes.root}>
255 <div className={classes.tree}>
257 pickerId={this.props.commandInput.id}
261 toggleItemSelection={this.refreshFiles} />
264 <div className={classes.chips}>
265 <Typography variant='subheading'>Selected files ({this.state.files.length}):</Typography>
269 values={this.state.files}
270 onChange={this.setFiles}
271 getLabel={(file: CollectionFile) => file.name} />
278 type DialogContentCssRules = 'root' | 'tree' | 'divider' | 'chips';