init load tags for collection
[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, TextField, Button
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 * as CopyToClipboard from 'react-copy-to-clipboard';
18 import { createCollectionTag } from '../../store/collection-panel/collection-panel-action';
19 import { LinkResource } from '../../models/link';
20
21 type CssRules = 'card' | 'iconHeader' | 'tag' | 'copyIcon';
22
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
24     card: {
25         marginBottom: '20px'
26     },
27     iconHeader: {
28         fontSize: '1.875rem',
29         color: theme.customs.colors.yellow700
30     },
31     tag: {
32         marginRight: theme.spacing.unit
33     },
34     copyIcon: {
35         marginLeft: theme.spacing.unit,
36         fontSize: '1.125rem',
37         color: theme.palette.grey["500"],
38         cursor: 'pointer'
39     }
40 });
41
42 interface CollectionPanelDataProps {
43     item: CollectionResource;
44     tags: LinkResource[];
45 }
46
47 interface CollectionPanelActionProps {
48     onItemRouteChange: (collectionId: string) => void;
49     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: CollectionResource) => void;
50 }
51
52 type CollectionPanelProps = CollectionPanelDataProps & CollectionPanelActionProps & DispatchProp
53                             & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
54
55
56 export const CollectionPanel = withStyles(styles)(
57     connect((state: RootState) => ({ 
58         item: state.collectionPanel.item, 
59         tags: state.collectionPanel.tags 
60     }))(
61         class extends React.Component<CollectionPanelProps> { 
62
63             render() {
64                 const { classes, item, tags, onContextMenu } = this.props;
65                 return <div>
66                         <Card className={classes.card}>
67                             <CardHeader 
68                                 avatar={ <CollectionIcon className={classes.iconHeader} /> }
69                                 action={ 
70                                     <IconButton
71                                         aria-label="More options"
72                                         onClick={event => onContextMenu(event, item)}>
73                                         <MoreOptionsIcon />
74                                     </IconButton> 
75                                 }
76                                 title={item && item.name } 
77                                 subheader={item && item.description} />
78                             <CardContent>
79                                 <Grid container direction="column">
80                                     <Grid item xs={6}>
81                                     <DetailsAttribute label='Collection UUID' value={item && item.uuid}>
82                                         <CopyToClipboard text={item && item.uuid}>
83                                             <CopyIcon className={classes.copyIcon} />
84                                         </CopyToClipboard>
85                                     </DetailsAttribute>
86                                     <DetailsAttribute label='Content size' value='54 MB' />
87                                     <DetailsAttribute label='Owner' value={item && item.ownerUuid} />
88                                     </Grid>
89                                 </Grid>
90                             </CardContent>
91                         </Card>
92
93                         <Card className={classes.card}>
94                             <CardHeader title="Tags" />
95                             <CardContent>
96                                 <Grid container direction="column">
97                                     <Grid item xs={12}>
98                                         {/* Temporarty button to add new tag */}
99                                         <Button onClick={this.addTag}>Add Tag</Button>
100                                     </Grid>
101                                     <Grid item xs={12}>
102                                         {
103                                             tags.map(tag => {
104                                                 return <Chip key={tag.etag} className={classes.tag}
105                                                     label={renderTagLabel(tag)}  />;
106                                             })
107                                         }
108                                     </Grid>
109                                 </Grid>
110                             </CardContent>
111                         </Card>
112
113                         <Card className={classes.card}>
114                             <CardHeader title="Files" />
115                             <CardContent>
116                                 <Grid container direction="column">
117                                     <Grid item xs={4}>
118                                         Tags
119                                     </Grid>
120                                 </Grid>
121                             </CardContent>
122                         </Card>
123                     </div>;
124             }
125
126             // Temporary method to add new tag
127             addTag = () => {
128                 this.props.dispatch<any>(createCollectionTag(this.props.item.uuid, 'dodalem nowy'));
129             }
130
131             componentWillReceiveProps({ match, item, onItemRouteChange }: CollectionPanelProps) {
132                 if (!item || match.params.id !== item.uuid) {
133                     onItemRouteChange(match.params.id);
134                 }
135             }
136
137         }
138     )
139 );
140
141 const renderTagLabel = (tag: LinkResource) => {
142     const { properties } = tag;
143     return `${properties.key}: ${properties.value}`;
144 };