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 } from '@mui/material';
10 import { WithStyles } from '@mui/styles';
11 import withStyles from '@mui/styles/withStyles';
13 GenericCommandInputParameter
14 } from 'models/workflow';
15 import { GenericInput, GenericInputProps } from './generic-input';
16 import { ProjectsTreePicker } from 'views-components/projects-tree-picker/projects-tree-picker';
17 import { initProjectsTreePicker } from 'store/tree-picker/tree-picker-actions';
18 import { TreeItem } from 'components/tree/tree';
19 import { ProjectsTreePickerItem } from 'store/tree-picker/tree-picker-middleware';
20 import { ProjectResource } from 'models/project';
21 import { ResourceKind } from 'models/resource';
22 import { RootState } from 'store/store';
23 import { getUserUuid } from 'common/getuser';
25 export type ProjectCommandInputParameter = GenericCommandInputParameter<ProjectResource, ProjectResource>;
27 const isUndefined: any = (value?: ProjectResource) => (value === undefined);
29 export interface ProjectInputProps {
31 input: ProjectCommandInputParameter;
32 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
35 type DialogContentCssRules = 'root' | 'pickerWrapper';
37 export const ProjectInput = ({ required, input, options }: ProjectInputProps) =>
41 component={ProjectInputComponent as any}
43 validate={required ? isUndefined : undefined}
49 const format = (value?: ProjectResource) => value ? value.name : '';
51 interface ProjectInputComponentState {
53 project?: ProjectResource;
56 interface HasUserUuid {
60 const mapStateToProps = (state: RootState) => ({ userUuid: getUserUuid(state) });
62 export const ProjectInputComponent = connect(mapStateToProps)(
63 class ProjectInputComponent extends React.Component<GenericInputProps & DispatchProp & HasUserUuid & {
64 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
66 }, ProjectInputComponentState> {
67 state: ProjectInputComponentState = {
72 this.props.dispatch<any>(
73 initProjectsTreePicker(this.props.commandInput.id));
84 this.componentDidMount();
85 this.setState({ open: true });
89 this.setState({ open: false });
94 this.props.input.onChange(this.state.project);
97 setProject = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
98 if ('kind' in data && data.kind === ResourceKind.PROJECT) {
99 this.setState({ project: data });
101 this.setState({ project: undefined });
105 invalid = () => (!this.state.project || !this.state.project.canWrite);
113 value={props.input.value}
114 error={props.meta.touched && !!props.meta.error}
115 disabled={props.commandInput.disabled}
116 onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
117 onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined} />}
121 dialogContentStyles: CustomStyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
124 flexDirection: 'column',
129 flexDirection: 'column',
134 dialog = withStyles(this.dialogContentStyles)(
135 ({ classes }: WithStyles<DialogContentCssRules>) =>
136 this.state.open ? <Dialog
137 open={this.state.open}
138 onClose={this.closeDialog}
140 data-cy="choose-a-project-dialog"
142 <DialogTitle>Choose a project</DialogTitle>
143 <DialogContent className={classes.root}>
144 <div className={classes.pickerWrapper}>
146 pickerId={this.props.commandInput.id}
147 cascadeSelection={false}
148 options={this.props.options}
149 toggleItemActive={this.setProject} />
153 <Button onClick={this.closeDialog}>Cancel</Button>
155 disabled={this.invalid()}
158 onClick={this.submit}>Ok</Button>