Switch the tagging adding properties) to use the collections API
[arvados-workbench2.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, Tooltip
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 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
23 import { getResource } from '~/store/resources/resources';
24 import { openContextMenu } from '~/store/context-menu/context-menu-actions';
25 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
26
27 type CssRules = 'card' | 'iconHeader' | 'tag' | 'copyIcon' | 'label' | 'value';
28
29 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
30     card: {
31         marginBottom: theme.spacing.unit * 2
32     },
33     iconHeader: {
34         fontSize: '1.875rem',
35         color: theme.customs.colors.yellow700
36     },
37     tag: {
38         marginRight: theme.spacing.unit,
39         marginBottom: theme.spacing.unit
40     },
41     copyIcon: {
42         marginLeft: theme.spacing.unit,
43         fontSize: '1.125rem',
44         color: theme.palette.grey["500"],
45         cursor: 'pointer'
46     },
47     label: {
48         fontSize: '0.875rem'
49     },
50     value: {
51         textTransform: 'none',
52         fontSize: '0.875rem'
53     }
54 });
55
56 interface CollectionPanelDataProps {
57     item: CollectionResource;
58     tags: TagResource[];
59 }
60
61 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
62     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
63
64
65 export const CollectionPanel = withStyles(styles)(
66     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
67         const collection = getResource(props.match.params.id)(state.resources);
68         return {
69             item: collection
70         };
71     })(
72         class extends React.Component<CollectionPanelProps> {
73             render() {
74                 const { classes, item, tags } = this.props;
75                 return <div>
76                     <Card className={classes.card}>
77                         <CardHeader
78                             avatar={<CollectionIcon className={classes.iconHeader} />}
79                             action={
80                                 <Tooltip title="More options">
81                                     <IconButton
82                                         aria-label="More options"
83                                         onClick={this.handleContextMenu}>
84                                         <MoreOptionsIcon />
85                                     </IconButton>
86                                 </Tooltip>
87                             }
88                             title={item && item.name}
89                             subheader={item && item.description} />
90                         <CardContent>
91                             <Grid container direction="column">
92                                 <Grid item xs={6}>
93                                     <DetailsAttribute classLabel={classes.label} classValue={classes.value}
94                                         label='Collection UUID'
95                                         value={item && item.uuid}>
96                                         <Tooltip title="Copy uuid">
97                                             <CopyToClipboard text={item && item.uuid} onCopy={() => this.onCopy()}>
98                                                 <CopyIcon className={classes.copyIcon} />
99                                             </CopyToClipboard>
100                                         </Tooltip>
101                                     </DetailsAttribute>
102                                     <DetailsAttribute classLabel={classes.label} classValue={classes.value}
103                                         label='Number of files' value='14' />
104                                     <DetailsAttribute classLabel={classes.label} classValue={classes.value}
105                                         label='Content size' value='54 MB' />
106                                     <DetailsAttribute classLabel={classes.label} classValue={classes.value}
107                                         label='Owner' value={item && item.ownerUuid} />
108                                 </Grid>
109                             </Grid>
110                         </CardContent>
111                     </Card>
112
113                     <Card className={classes.card}>
114                         <CardHeader title="Properties" />
115                         <CardContent>
116                             <Grid container direction="column">
117                                 <Grid item xs={12}><CollectionTagForm /></Grid>
118                                 <Grid item xs={12}>
119                                     {
120                                         Object.keys(item.properties).map( key => {
121                                             return <Chip key={key} className={classes.tag}
122                                                 onDelete={this.handleDelete(key)}
123                                                 label={`${key}: ${item.properties[key]}`} />;
124                                         })
125                                     }
126                                 </Grid>
127                             </Grid>
128                         </CardContent>
129                     </Card>
130                     <div className={classes.card}>
131                         <CollectionPanelFiles />
132                     </div>
133                 </div>;
134             }
135
136             handleContextMenu = (event: React.MouseEvent<any>) => {
137                 const { uuid, ownerUuid, name, description, kind } = this.props.item;
138                 const resource = {
139                     uuid,
140                     ownerUuid,
141                     name,
142                     description,
143                     kind,
144                     menuKind: ContextMenuKind.COLLECTION
145                 };
146                 this.props.dispatch<any>(openContextMenu(event, resource));
147             }
148
149             handleDelete = (key: string) => () => {
150                 this.props.dispatch<any>(deleteCollectionTag(key));
151             }
152
153             onCopy = () => {
154                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
155                     message: "Uuid has been copied",
156                     hideDuration: 2000
157                 }));
158             }
159         }
160     )
161 );