7a4cfba6c56e5d133c2a61a94101f33c2d01cd3f
[arvados-workbench2.git] / src / views-components / project-properties-dialog / project-properties-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from "react";
6 import { Dispatch } from "redux";
7 import { connect } from "react-redux";
8 import { RootState } from '~/store/store';
9 import { withDialog, WithDialogProps } from "~/store/dialog/with-dialog";
10 import { ProjectResource } from '~/models/project';
11 import { PROJECT_PROPERTIES_DIALOG_NAME, deleteProjectProperty } from '~/store/details-panel/details-panel-action';
12 import { Dialog, DialogTitle, DialogContent, DialogActions, Button, withStyles, StyleRulesCallback, WithStyles } from '@material-ui/core';
13 import { ArvadosTheme } from '~/common/custom-theme';
14 import { ProjectPropertiesForm } from '~/views-components/project-properties-dialog/project-properties-form';
15 import { getResource } from '~/store/resources/resources';
16 import { PropertyChipComponent } from "../resource-properties-form/property-chip";
17
18 type CssRules = 'tag';
19
20 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
21     tag: {
22         marginRight: theme.spacing.unit,
23         marginBottom: theme.spacing.unit
24     }
25 });
26
27 interface ProjectPropertiesDialogDataProps {
28     project: ProjectResource;
29 }
30
31 interface ProjectPropertiesDialogActionProps {
32     handleDelete: (key: string) => void;
33 }
34
35 const mapStateToProps = ({ detailsPanel, resources, properties }: RootState): ProjectPropertiesDialogDataProps => ({
36     project: getResource(detailsPanel.resourceUuid)(resources) as ProjectResource,
37 });
38
39 const mapDispatchToProps = (dispatch: Dispatch): ProjectPropertiesDialogActionProps => ({
40     handleDelete: (key: string) => dispatch<any>(deleteProjectProperty(key)),
41 });
42
43 type ProjectPropertiesDialogProps =  ProjectPropertiesDialogDataProps & ProjectPropertiesDialogActionProps & WithDialogProps<{}> & WithStyles<CssRules>;
44
45 export const ProjectPropertiesDialog = connect(mapStateToProps, mapDispatchToProps)(
46     withStyles(styles)(
47     withDialog(PROJECT_PROPERTIES_DIALOG_NAME)(
48         ({ classes, open, closeDialog, handleDelete, project }: ProjectPropertiesDialogProps) =>
49             <Dialog open={open}
50                 onClose={closeDialog}
51                 fullWidth
52                 maxWidth='sm'>
53                 <DialogTitle>Properties</DialogTitle>
54                 <DialogContent>
55                     <ProjectPropertiesForm />
56                     {project && project.properties &&
57                         Object.keys(project.properties).map(k =>
58                             <PropertyChipComponent
59                                 onDelete={() => handleDelete(k)}
60                                 key={k} className={classes.tag}
61                                 propKey={k} propValue={project.properties[k]} />)
62                     }
63                 </DialogContent>
64                 <DialogActions>
65                     <Button
66                         variant='text'
67                         color='primary'
68                         onClick={closeDialog}>
69                         Close
70                     </Button>
71                 </DialogActions>
72             </Dialog>
73     )
74 ));