18559: Update the new user dialog and fix groups functionality
[arvados-workbench2.git] / src / views-components / dialog-create / dialog-project-create.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { InjectedFormProps } from 'redux-form';
7 import { WithDialogProps } from 'store/dialog/with-dialog';
8 import { ProjectCreateFormDialogData, PROJECT_CREATE_FORM_NAME } from 'store/projects/project-create-actions';
9 import { FormDialog } from 'components/form-dialog/form-dialog';
10 import { ProjectNameField, ProjectDescriptionField, UsersField } from 'views-components/form-fields/project-form-fields';
11 import { CreateProjectPropertiesForm } from 'views-components/project-properties/create-project-properties-form';
12 import { ResourceParentField } from '../form-fields/resource-form-fields';
13 import { FormGroup, FormLabel, StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
14 import { resourcePropertiesList } from 'views-components/resource-properties/resource-properties-list';
15 import { GroupClass } from 'models/group';
16
17 type CssRules = 'propertiesForm' | 'description';
18
19 const styles: StyleRulesCallback<CssRules> = theme => ({
20     propertiesForm: {
21         marginTop: theme.spacing.unit * 2,
22         marginBottom: theme.spacing.unit * 2,
23     },
24     description: {
25         marginTop: theme.spacing.unit * 2,
26         marginBottom: theme.spacing.unit * 2,
27     },
28 });
29
30 type DialogProjectProps = WithDialogProps<{sourcePanel: GroupClass}> & InjectedFormProps<ProjectCreateFormDialogData>;
31
32 export const DialogProjectCreate = (props: DialogProjectProps) => {
33     let title = 'New Project';
34     let fields = ProjectAddFields;
35     const sourcePanel = props.data.sourcePanel || '';
36
37     if (sourcePanel === GroupClass.ROLE) {
38         title = 'New Group';
39         fields = GroupAddFields;
40     }
41
42     return <FormDialog
43         dialogTitle={title}
44         formFields={fields as any}
45         submitLabel='Create'
46         {...props}
47     />;
48 };
49
50 const CreateProjectPropertiesList = resourcePropertiesList(PROJECT_CREATE_FORM_NAME);
51
52 const ProjectAddFields = withStyles(styles)(
53     ({ classes }: WithStyles<CssRules>) => <span>
54         <ResourceParentField />
55         <ProjectNameField />
56         <div className={classes.description}>
57             <ProjectDescriptionField />
58         </div>
59         <div className={classes.propertiesForm}>
60             <FormLabel>Properties</FormLabel>
61             <FormGroup>
62                 <CreateProjectPropertiesForm />
63                 <CreateProjectPropertiesList />
64             </FormGroup>
65         </div>
66     </span>);
67
68 const GroupAddFields = withStyles(styles)(
69     ({ classes }: WithStyles<CssRules>) => <span>
70         <ProjectNameField />
71         <UsersField />
72         <div className={classes.description}>
73             <ProjectDescriptionField />
74         </div>
75         <div className={classes.propertiesForm}>
76             <FormLabel>Properties</FormLabel>
77             <FormGroup>
78                 <CreateProjectPropertiesForm />
79                 <CreateProjectPropertiesList />
80             </FormGroup>
81         </div>
82     </span>);