Merge branch 'master' into 14039-details-view-improvements
[arvados.git] / src / views / collection-panel / collection-panel.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 {
7     StyleRulesCallback, WithStyles, withStyles, Card,
8     CardHeader, IconButton, CardContent, Grid, Chip
9 } from '@material-ui/core';
10 import { connect, DispatchProp } from "react-redux";
11 import { RouteComponentProps } from 'react-router';
12 import { ArvadosTheme } from '~/common/custom-theme';
13 import { RootState } from '~/store/store';
14 import { MoreOptionsIcon, CollectionIcon, CopyIcon } from '~/components/icon/icon';
15 import { DetailsAttribute } from '~/components/details-attribute/details-attribute';
16 import { CollectionResource } from '~/models/collection';
17 import { CollectionPanelFiles } from '~/views-components/collection-panel-files/collection-panel-files';
18 import * as CopyToClipboard from 'react-copy-to-clipboard';
19 import { TagResource } from '~/models/tag';
20 import { CollectionTagForm } from './collection-tag-form';
21 import { deleteCollectionTag } from '~/store/collection-panel/collection-panel-action';
22
23 type CssRules = 'card' | 'iconHeader' | 'tag' | 'copyIcon' | 'label' | 'value';
24
25 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
26     card: {
27         marginBottom: theme.spacing.unit * 2
28     },
29     iconHeader: {
30         fontSize: '1.875rem',
31         color: theme.customs.colors.yellow700
32     },
33     tag: {
34         marginRight: theme.spacing.unit,
35         marginBottom: theme.spacing.unit
36     },
37     copyIcon: {
38         marginLeft: theme.spacing.unit,
39         fontSize: '1.125rem',
40         color: theme.palette.grey["500"],
41         cursor: 'pointer'
42     },
43     label: {
44         fontSize: '0.875rem'
45     },
46     value: {
47         textTransform: 'none',
48         fontSize: '0.875rem'
49     }
50 });
51
52 interface CollectionPanelDataProps {
53     item: CollectionResource;
54     tags: TagResource[];
55 }
56
57 interface CollectionPanelActionProps {
58     onItemRouteChange: (collectionId: string) => void;
59     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: CollectionResource) => void;
60 }
61
62 type CollectionPanelProps = CollectionPanelDataProps & CollectionPanelActionProps & DispatchProp
63                             & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
64
65
66 export const CollectionPanel = withStyles(styles)(
67     connect((state: RootState) => ({
68         item: state.collectionPanel.item,
69         tags: state.collectionPanel.tags
70     }))(
71         class extends React.Component<CollectionPanelProps> {
72
73             render() {
74                 const { classes, item, tags, onContextMenu } = this.props;
75                 return <div>
76                         <Card className={classes.card}>
77                             <CardHeader
78                                 avatar={ <CollectionIcon className={classes.iconHeader} /> }
79                                 action={
80                                     <IconButton
81                                         aria-label="More options"
82                                         onClick={event => onContextMenu(event, item)}>
83                                         <MoreOptionsIcon />
84                                     </IconButton>
85                                 }
86                                 title={item && item.name }
87                                 subheader={item && item.description} />
88                             <CardContent>
89                                 <Grid container direction="column">
90                                     <Grid item xs={6}>
91                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
92                                                 label='Collection UUID'
93                                                 value={item && item.uuid}>
94                                             <CopyToClipboard text={item && item.uuid}>
95                                                 <CopyIcon className={classes.copyIcon} />
96                                             </CopyToClipboard>
97                                         </DetailsAttribute>
98                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value} 
99                                             label='Number of files' value='14' />
100                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value} 
101                                             label='Content size' value='54 MB' />
102                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value} 
103                                             label='Owner' value={item && item.ownerUuid} />
104                                     </Grid>
105                                 </Grid>
106                             </CardContent>
107                         </Card>
108
109                         <Card className={classes.card}>
110                             <CardHeader title="Properties" />
111                             <CardContent>
112                                 <Grid container direction="column">
113                                     <Grid item xs={12}><CollectionTagForm /></Grid>
114                                     <Grid item xs={12}>
115                                         {
116                                             tags.map(tag => {
117                                                 return <Chip key={tag.etag} className={classes.tag}
118                                                     onDelete={this.handleDelete(tag.uuid)}
119                                                     label={renderTagLabel(tag)}  />;
120                                             })
121                                         }
122                                     </Grid>
123                                 </Grid>
124                             </CardContent>
125                         </Card>
126                         <div className={classes.card}>
127                             <CollectionPanelFiles/>
128                         </div>
129                     </div>;
130             }
131
132             handleDelete = (uuid: string) => () => {
133                 this.props.dispatch<any>(deleteCollectionTag(uuid));
134             }
135
136             componentWillReceiveProps({ match, item, onItemRouteChange }: CollectionPanelProps) {
137                 if (!item || match.params.id !== item.uuid) {
138                     onItemRouteChange(match.params.id);
139                 }
140             }
141
142         }
143     )
144 );
145
146 const renderTagLabel = (tag: TagResource) => {
147     const { properties } = tag;
148     return `${properties.key}: ${properties.value}`;
149 };