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 { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button, withStyles, WithStyles, StyleRulesCallback } from '@material-ui/core';
10 GenericCommandInputParameter
11 } from 'models/workflow';
12 import { GenericInput, GenericInputProps } from './generic-input';
13 import { ProjectsTreePicker } from 'views-components/projects-tree-picker/projects-tree-picker';
14 import { initProjectsTreePicker } from 'store/tree-picker/tree-picker-actions';
15 import { TreeItem } from 'components/tree/tree';
16 import { ProjectsTreePickerItem } from 'store/tree-picker/tree-picker-middleware';
17 import { ProjectResource } from 'models/project';
18 import { ResourceKind } from 'models/resource';
19 import { RootState } from 'store/store';
20 import { getUserUuid } from 'common/getuser';
22 export type ProjectCommandInputParameter = GenericCommandInputParameter<ProjectResource, ProjectResource>;
24 const isUndefined: any = (value?: ProjectResource) => (value === undefined);
26 export interface ProjectInputProps {
28 input: ProjectCommandInputParameter;
29 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
32 type DialogContentCssRules = 'root' | 'pickerWrapper';
34 export const ProjectInput = ({ required, input, options }: ProjectInputProps) =>
38 component={ProjectInputComponent as any}
40 validate={required ? isUndefined : undefined}
46 const format = (value?: ProjectResource) => value ? value.name : '';
48 interface ProjectInputComponentState {
50 project?: ProjectResource;
53 interface HasUserUuid {
57 const mapStateToProps = (state: RootState) => ({ userUuid: getUserUuid(state) });
59 export const ProjectInputComponent = connect(mapStateToProps)(
60 class ProjectInputComponent extends React.Component<GenericInputProps & DispatchProp & HasUserUuid & {
61 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
63 }, ProjectInputComponentState> {
64 state: ProjectInputComponentState = {
69 this.props.dispatch<any>(
70 initProjectsTreePicker(this.props.commandInput.id));
81 this.componentDidMount();
82 this.setState({ open: true });
86 this.setState({ open: false });
91 this.props.input.onChange(this.state.project);
94 setProject = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
95 if ('kind' in data && data.kind === ResourceKind.PROJECT) {
96 this.setState({ project: data });
98 this.setState({ project: undefined });
102 invalid = () => (!this.state.project || !this.state.project.canWrite);
110 value={props.input.value}
111 error={props.meta.touched && !!props.meta.error}
112 disabled={props.commandInput.disabled}
113 onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
114 onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined} />}
118 dialogContentStyles: StyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
121 flexDirection: 'column',
124 flexBasis: `${spacing.unit * 8}vh`,
128 flexDirection: 'column',
132 dialog = withStyles(this.dialogContentStyles)(
133 ({ classes }: WithStyles<DialogContentCssRules>) =>
134 this.state.open ? <Dialog
135 open={this.state.open}
136 onClose={this.closeDialog}
138 data-cy="choose-a-project-dialog"
140 <DialogTitle>Choose a project</DialogTitle>
141 <DialogContent className={classes.root}>
142 <div className={classes.pickerWrapper}>
144 pickerId={this.props.commandInput.id}
145 cascadeSelection={false}
146 options={this.props.options}
147 toggleItemActive={this.setProject} />
151 <Button onClick={this.closeDialog}>Cancel</Button>
153 disabled={this.invalid()}
156 onClick={this.submit}>Ok</Button>