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 require: any = (value?: ProjectResource) => (value === undefined);
26 export interface ProjectInputProps {
27 input: ProjectCommandInputParameter;
28 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
31 type DialogContentCssRules = 'root' | 'pickerWrapper';
33 export const ProjectInput = ({ input, options }: ProjectInputProps) =>
37 component={ProjectInputComponent as any}
44 const format = (value?: ProjectResource) => value ? value.name : '';
46 interface ProjectInputComponentState {
48 project?: ProjectResource;
51 interface HasUserUuid {
55 const mapStateToProps = (state: RootState) => ({ userUuid: getUserUuid(state) });
57 export const ProjectInputComponent = connect(mapStateToProps)(
58 class ProjectInputComponent extends React.Component<GenericInputProps & DispatchProp & HasUserUuid & {
59 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
60 }, ProjectInputComponentState> {
61 state: ProjectInputComponentState = {
66 this.props.dispatch<any>(
67 initProjectsTreePicker(this.props.commandInput.id));
78 this.componentDidMount();
79 this.setState({ open: true });
83 this.setState({ open: false });
88 this.props.input.onChange(this.state.project);
91 setProject = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
92 if ('kind' in data && data.kind === ResourceKind.PROJECT) {
93 this.setState({ project: data });
95 this.setState({ project: undefined });
99 invalid = () => (!this.state.project || this.state.project.writableBy.indexOf(this.props.userUuid) === -1);
107 value={props.input.value}
108 error={props.meta.touched && !!props.meta.error}
109 disabled={props.commandInput.disabled}
110 onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
111 onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined} />}
115 dialogContentStyles: StyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
118 flexDirection: 'column',
121 flexBasis: `${spacing.unit * 8}vh`,
127 dialog = withStyles(this.dialogContentStyles)(
128 ({ classes }: WithStyles<DialogContentCssRules>) =>
129 this.state.open ? <Dialog
130 open={this.state.open}
131 onClose={this.closeDialog}
133 data-cy="choose-a-project-dialog"
135 <DialogTitle>Choose a project</DialogTitle>
136 <DialogContent className={classes.root}>
137 <div className={classes.pickerWrapper}>
139 pickerId={this.props.commandInput.id}
140 options={this.props.options}
141 toggleItemActive={this.setProject} />
145 <Button onClick={this.closeDialog}>Cancel</Button>
147 disabled={this.invalid()}
150 onClick={this.submit}>Ok</Button>