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 } 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 };
30 export const ProjectInput = ({ input, options }: ProjectInputProps) =>
34 component={ProjectInputComponent as any}
41 const format = (value?: ProjectResource) => value ? value.name : '';
43 interface ProjectInputComponentState {
45 project?: ProjectResource;
48 interface HasUserUuid {
52 const mapStateToProps = (state: RootState) => ({ userUuid: getUserUuid(state) });
54 export const ProjectInputComponent = connect(mapStateToProps)(
55 class ProjectInputComponent extends React.Component<GenericInputProps & DispatchProp & HasUserUuid & {
56 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
57 }, ProjectInputComponentState> {
58 state: ProjectInputComponentState = {
63 this.props.dispatch<any>(
64 initProjectsTreePicker(this.props.commandInput.id));
75 this.componentDidMount();
76 this.setState({ open: true });
80 this.setState({ open: false });
85 this.props.input.onChange(this.state.project);
88 setProject = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
89 if ('kind' in data && data.kind === ResourceKind.PROJECT) {
90 this.setState({ project: data });
92 this.setState({ project: undefined });
96 invalid = () => (!this.state.project || this.state.project.writableBy.indexOf(this.props.userUuid) === -1);
104 value={props.input.value}
105 error={props.meta.touched && !!props.meta.error}
106 disabled={props.commandInput.disabled}
107 onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
108 onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined} />}
113 return this.state.open ? <Dialog
114 open={this.state.open}
115 onClose={this.closeDialog}
117 data-cy="choose-a-project-dialog"
119 <DialogTitle>Choose a project</DialogTitle>
122 pickerId={this.props.commandInput.id}
123 options={this.props.options}
124 toggleItemActive={this.setProject} />
127 <Button onClick={this.closeDialog}>Cancel</Button>
129 disabled={this.invalid()}
132 onClick={this.submit}>Ok</Button>