Merge branch '14258-collection-filtering'
[arvados.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, Chip, 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
17 type CssRules = 'tag';
18
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
20     tag: {
21         marginRight: theme.spacing.unit,
22         marginBottom: theme.spacing.unit
23     }
24 });
25
26 interface ProjectPropertiesDialogDataProps {
27     project: ProjectResource;
28 }
29
30 interface ProjectPropertiesDialogActionProps {
31     handleDelete: (key: string) => void;
32 }
33
34 const mapStateToProps = ({ detailsPanel, resources }: RootState): ProjectPropertiesDialogDataProps => {
35     const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
36     return { project };
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                             return <Chip key={k} className={classes.tag}
59                                 onDelete={() => handleDelete(k)}
60                                 label={`${k}: ${project.properties[k]}`} />;
61                         })
62                     }
63                 </DialogContent>
64                 <DialogActions>
65                     <Button
66                         variant='flat'
67                         color='primary'
68                         onClick={closeDialog}>
69                         Close
70                     </Button>
71                 </DialogActions>
72             </Dialog>
73 )));