1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { connect, DispatchProp } from 'react-redux';
7 import { Field } from 'redux-form';
8 import { CustomStyleRulesCallback } from 'common/custom-theme';
9 import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button, withStyles, WithStyles } from '@material-ui/core';
11 GenericCommandInputParameter
12 } from 'models/workflow';
13 import { GenericInput, GenericInputProps } from './generic-input';
14 import { ProjectsTreePicker } from 'views-components/projects-tree-picker/projects-tree-picker';
15 import { initProjectsTreePicker } from 'store/tree-picker/tree-picker-actions';
16 import { TreeItem } from 'components/tree/tree';
17 import { ProjectsTreePickerItem } from 'store/tree-picker/tree-picker-middleware';
18 import { ProjectResource } from 'models/project';
19 import { ResourceKind } from 'models/resource';
20 import { RootState } from 'store/store';
21 import { getUserUuid } from 'common/getuser';
23 export type ProjectCommandInputParameter = GenericCommandInputParameter<ProjectResource, ProjectResource>;
25 const isUndefined: any = (value?: ProjectResource) => (value === undefined);
27 export interface ProjectInputProps {
29 input: ProjectCommandInputParameter;
30 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
33 type DialogContentCssRules = 'root' | 'pickerWrapper';
35 export const ProjectInput = ({ required, input, options }: ProjectInputProps) =>
39 component={ProjectInputComponent as any}
41 validate={required ? isUndefined : undefined}
47 const format = (value?: ProjectResource) => value ? value.name : '';
49 interface ProjectInputComponentState {
51 project?: ProjectResource;
54 interface HasUserUuid {
58 const mapStateToProps = (state: RootState) => ({ userUuid: getUserUuid(state) });
60 export const ProjectInputComponent = connect(mapStateToProps)(
61 class ProjectInputComponent extends React.Component<GenericInputProps & DispatchProp & HasUserUuid & {
62 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
64 }, ProjectInputComponentState> {
65 state: ProjectInputComponentState = {
70 this.props.dispatch<any>(
71 initProjectsTreePicker(this.props.commandInput.id));
82 this.componentDidMount();
83 this.setState({ open: true });
87 this.setState({ open: false });
92 this.props.input.onChange(this.state.project);
95 setProject = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
96 if ('kind' in data && data.kind === ResourceKind.PROJECT) {
97 this.setState({ project: data });
99 this.setState({ project: undefined });
103 invalid = () => (!this.state.project || !this.state.project.canWrite);
111 value={props.input.value}
112 error={props.meta.touched && !!props.meta.error}
113 disabled={props.commandInput.disabled}
114 onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
115 onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined} />}
119 dialogContentStyles: CustomStyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
122 flexDirection: 'column',
125 flexBasis: `${spacing(8)}vh`,
129 flexDirection: 'column',
133 dialog = withStyles(this.dialogContentStyles)(
134 ({ classes }: WithStyles<DialogContentCssRules>) =>
135 this.state.open ? <Dialog
136 open={this.state.open}
137 onClose={this.closeDialog}
139 data-cy="choose-a-project-dialog"
141 <DialogTitle>Choose a project</DialogTitle>
142 <DialogContent className={classes.root}>
143 <div className={classes.pickerWrapper}>
145 pickerId={this.props.commandInput.id}
146 cascadeSelection={false}
147 options={this.props.options}
148 toggleItemActive={this.setProject} />
152 <Button onClick={this.closeDialog}>Cancel</Button>
154 disabled={this.invalid()}
157 onClick={this.submit}>Ok</Button>