// Copyright (C) The Arvados Authors. All rights reserved.
//
// SPDX-License-Identifier: AGPL-3.0
import React from 'react';
import { connect, DispatchProp } from 'react-redux';
import { memoize } from 'lodash/fp';
import { Field } from 'redux-form';
import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button, StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core';
import {
isRequiredInput,
DirectoryCommandInputParameter,
CWLType,
Directory
} from 'models/workflow';
import { GenericInputProps, GenericInput } from './generic-input';
import { ProjectsTreePicker } from 'views-components/projects-tree-picker/projects-tree-picker';
import { FileOperationLocation, getFileOperationLocation, initProjectsTreePicker } from 'store/tree-picker/tree-picker-actions';
import { TreeItem } from 'components/tree/tree';
import { ProjectsTreePickerItem } from 'store/tree-picker/tree-picker-middleware';
import { ERROR_MESSAGE } from 'validators/require';
import { Dispatch } from 'redux';
export interface DirectoryInputProps {
input: DirectoryCommandInputParameter;
options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
}
type DialogContentCssRules = 'root' | 'pickerWrapper';
export const DirectoryInput = ({ input, options }: DirectoryInputProps) =>
;
const format = (value?: Directory) => value ? value.basename : '';
const parse = (directory: FileOperationLocation): Directory => ({
class: CWLType.DIRECTORY,
location: `keep:${directory.pdh}${directory.subpath}`,
basename: directory.name,
});
const getValidation = memoize(
(input: DirectoryCommandInputParameter) => ([
isRequiredInput(input)
? (directory?: Directory) => directory ? undefined : ERROR_MESSAGE
: () => undefined,
])
);
interface DirectoryInputComponentState {
open: boolean;
directory?: FileOperationLocation;
}
interface DirectoryInputActionProps {
initProjectsTreePicker: (pickerId: string) => void;
getFileOperationLocation: (item: ProjectsTreePickerItem) => Promise;
}
const mapDispatchToProps = (dispatch: Dispatch): DirectoryInputActionProps => ({
initProjectsTreePicker: (pickerId: string) => dispatch(initProjectsTreePicker(pickerId)),
getFileOperationLocation: (item: ProjectsTreePickerItem) => dispatch(getFileOperationLocation(item)),
});
const DirectoryInputComponent = connect(null, mapDispatchToProps)(
class FileInputComponent extends React.Component {
state: DirectoryInputComponentState = {
open: false,
};
componentDidMount() {
this.props.initProjectsTreePicker(this.props.commandInput.id);
}
render() {
return <>
{this.renderInput()}
>;
}
openDialog = () => {
this.setState({ open: true });
}
closeDialog = () => {
this.setState({ open: false });
}
submit = () => {
this.closeDialog();
this.props.input.onChange(this.state.directory);
}
setDirectory = async (_: {}, { data: item }: TreeItem) => {
const location = await this.props.getFileOperationLocation(item);
this.setState({ directory: location });
}
renderInput() {
return
}
{...this.props} />;
}
dialogContentStyles: StyleRulesCallback = ({ spacing }) => ({
root: {
display: 'flex',
flexDirection: 'column',
},
pickerWrapper: {
flexBasis: `${spacing.unit * 8}vh`,
flexShrink: 1,
minHeight: 0,
},
});
dialog = withStyles(this.dialogContentStyles)(
({ classes }: WithStyles) =>
);
});