Refactor to apply global navigation actions
[arvados-workbench2.git] / src / views / collection-panel / collection-panel.tsx
index 1e5fb5e8a6891185394c89655a96533350cc9db4..d22dd0de289122cdf088504a614f1042c6254161 100644 (file)
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { 
-    StyleRulesCallback, WithStyles, withStyles, Card, CardHeader, IconButton, 
-    CardContent, Grid, MenuItem, Menu, ListItemIcon, ListItemText, Typography 
+import {
+    StyleRulesCallback, WithStyles, withStyles, Card,
+    CardHeader, IconButton, CardContent, Grid, Chip, Tooltip
 } from '@material-ui/core';
-import { connect } from 'react-redux';
+import { connect, DispatchProp } from "react-redux";
 import { RouteComponentProps } from 'react-router';
-import { ArvadosTheme } from '../../common/custom-theme';
-import { RootState } from '../../store/store';
-import { 
-    MoreOptionsIcon, CollectionIcon, ShareIcon, RenameIcon, AddFavoriteIcon, MoveToIcon, 
-    CopyIcon, ProvenanceGraphIcon, DetailsIcon, AdvancedIcon, RemoveIcon 
-} from '../../components/icon/icon';
-import { DetailsAttribute } from '../../components/details-attribute/details-attribute';
-import { CollectionResource } from '../../models/collection';
-
-type CssRules = 'card' | 'iconHeader';
+import { ArvadosTheme } from '~/common/custom-theme';
+import { RootState } from '~/store/store';
+import { MoreOptionsIcon, CollectionIcon, CopyIcon } from '~/components/icon/icon';
+import { DetailsAttribute } from '~/components/details-attribute/details-attribute';
+import { CollectionResource } from '~/models/collection';
+import { CollectionPanelFiles } from '~/views-components/collection-panel-files/collection-panel-files';
+import * as CopyToClipboard from 'react-copy-to-clipboard';
+import { TagResource } from '~/models/tag';
+import { CollectionTagForm } from './collection-tag-form';
+import { deleteCollectionTag } from '~/store/collection-panel/collection-panel-action';
+import { snackbarActions } from '~/store/snackbar/snackbar-actions';
+import { getResource } from '~/store/resources/resources';
+import { contextMenuActions } from '~/store/context-menu/context-menu-actions';
+import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
+
+type CssRules = 'card' | 'iconHeader' | 'tag' | 'copyIcon' | 'label' | 'value';
 
 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     card: {
-        marginBottom: '20px'
+        marginBottom: theme.spacing.unit * 2
     },
     iconHeader: {
         fontSize: '1.875rem',
         color: theme.customs.colors.yellow700
-    }
-});
-
-const MENU_OPTIONS = [
-    {
-        title: 'Edit collection',
-        icon: RenameIcon
-    },
-    {
-        title: 'Share',
-        icon: ShareIcon
-    },
-    {
-        title: 'Move to',
-        icon: MoveToIcon
     },
-    {
-        title: 'Add to favorites',
-        icon: AddFavoriteIcon
+    tag: {
+        marginRight: theme.spacing.unit,
+        marginBottom: theme.spacing.unit
     },
-    {
-        title: 'Copy to project',
-        icon: CopyIcon
+    copyIcon: {
+        marginLeft: theme.spacing.unit,
+        fontSize: '1.125rem',
+        color: theme.palette.grey["500"],
+        cursor: 'pointer'
     },
-    {
-        title: 'View details',
-        icon: DetailsIcon
+    label: {
+        fontSize: '0.875rem'
     },
-    {
-        title: 'Provenance graph',
-        icon: ProvenanceGraphIcon
-    },
-    {
-        title: 'Advanced',
-        icon: AdvancedIcon
-    },
-    {
-        title: 'Remove',
-        icon: RemoveIcon
+    value: {
+        textTransform: 'none',
+        fontSize: '0.875rem'
     }
-];
+});
 
 interface CollectionPanelDataProps {
     item: CollectionResource;
+    tags: TagResource[];
 }
 
-interface CollectionPanelActionProps {
-    onItemRouteChange: (collectionId: string) => void;
-}
+type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
+    & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
 
-type CollectionPanelProps = CollectionPanelDataProps & CollectionPanelActionProps 
-                            & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
 
 export const CollectionPanel = withStyles(styles)(
-    connect((state: RootState) => ({ item: state.collectionPanel.item }))(
-        class extends React.Component<CollectionPanelProps> { 
-
-            state = {
-                anchorEl: undefined,
-                open: false
-            };
-
-            showMenu = (event: any) => {
-                this.setState({ anchorEl: event.currentTarget, open: true });
-            }
-
-            closeMenu = () => {
-                this.setState({ anchorEl: undefined, open: false });
-            }
-
-            displayMenuAction = () => {
-                return <IconButton
-                    aria-label="More options"
-                    aria-owns={this.state.anchorEl ? 'submenu' : undefined}
-                    aria-haspopup="true"
-                    onClick={this.showMenu}>
-                    <MoreOptionsIcon />
-                </IconButton>;
-            }
+    connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
+        const collection = getResource(props.match.params.id)(state.resources);
+        return {
+            item: collection,
+            tags: state.collectionPanel.tags
+        };
+    })(
+        class extends React.Component<CollectionPanelProps> {
 
             render() {
-                const { anchorEl, open } = this.state;
-                const { classes, item } = this.props;
+                const { classes, item, tags } = this.props;
                 return <div>
-                        <Card className={classes.card}>
-                            <CardHeader 
-                                avatar={ <CollectionIcon className={classes.iconHeader} /> }
-                                action={ 
-                                    <IconButton
-                                        aria-label="More options"
-                                        aria-owns={anchorEl ? 'submenu' : undefined}
-                                        aria-haspopup="true"
-                                        onClick={this.showMenu}>
-                                        <MoreOptionsIcon />
-                                    </IconButton> 
-                                }
-                                title={item && item.name } />
-                            <CardContent>
-                                <Menu
-                                    id="submenu"
-                                    anchorEl={anchorEl}
-                                    open={open}
-                                    onClose={this.closeMenu}>
-                                    {MENU_OPTIONS.map((option) => (
-                                        <MenuItem key={option.title}>
-                                            <ListItemIcon>
-                                                <option.icon />
-                                            </ListItemIcon>
-                                            <ListItemText inset primary={
-                                                <Typography variant='body1'>
-                                                    {option.title}
-                                                </Typography>
-                                            }/>
-                                        </MenuItem>
-                                    ))}
-                                </Menu>
-                                <Grid container direction="column">
-                                    <Grid item xs={6}>
-                                    <DetailsAttribute label='Collection UUID' value={item && item.uuid} />
-                                        <DetailsAttribute label='Content size' value='54 MB' />
-                                    <DetailsAttribute label='Owner' value={item && item.ownerUuid} />
-                                    </Grid>
+                    <Card className={classes.card}>
+                        <CardHeader
+                            avatar={<CollectionIcon className={classes.iconHeader} />}
+                            action={
+                                <IconButton
+                                    aria-label="More options"
+                                    onClick={this.handleContextMenu}>
+                                    <MoreOptionsIcon />
+                                </IconButton>
+                            }
+                            title={item && item.name}
+                            subheader={item && item.description} />
+                        <CardContent>
+                            <Grid container direction="column">
+                                <Grid item xs={6}>
+                                    <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                                        label='Collection UUID'
+                                        value={item && item.uuid}>
+                                        <Tooltip title="Copy uuid">
+                                            <CopyToClipboard text={item && item.uuid} onCopy={() => this.onCopy()}>
+                                                <CopyIcon className={classes.copyIcon} />
+                                            </CopyToClipboard>
+                                        </Tooltip>
+                                    </DetailsAttribute>
+                                    <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                                        label='Number of files' value='14' />
+                                    <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                                        label='Content size' value='54 MB' />
+                                    <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                                        label='Owner' value={item && item.ownerUuid} />
                                 </Grid>
-                            </CardContent>
-                        </Card>
-
-                        <Card className={classes.card}>
-                            <CardHeader title="Tags" />
-                            <CardContent>
-                                <Grid container direction="column">
-                                    <Grid item xs={4}>
-                                        Tags
-                                    </Grid>
+                            </Grid>
+                        </CardContent>
+                    </Card>
+
+                    <Card className={classes.card}>
+                        <CardHeader title="Properties" />
+                        <CardContent>
+                            <Grid container direction="column">
+                                <Grid item xs={12}><CollectionTagForm /></Grid>
+                                <Grid item xs={12}>
+                                    {
+                                        tags.map(tag => {
+                                            return <Chip key={tag.etag} className={classes.tag}
+                                                onDelete={this.handleDelete(tag.uuid)}
+                                                label={renderTagLabel(tag)} />;
+                                        })
+                                    }
                                 </Grid>
-                            </CardContent>
-                        </Card>
+                            </Grid>
+                        </CardContent>
+                    </Card>
+                    <div className={classes.card}>
+                        <CollectionPanelFiles />
+                    </div>
+                </div>;
+            }
 
-                        <Card className={classes.card}>
-                            <CardHeader title="Files" />
-                            <CardContent>
-                                <Grid container direction="column">
-                                    <Grid item xs={4}>
-                                        Tags
-                                    </Grid>
-                                </Grid>
-                            </CardContent>
-                        </Card>
-                    </div>;
+            handleContextMenu = (event: React.MouseEvent<any>) => {
+                event.preventDefault();
+                const { uuid, name, description } = this.props.item;
+                const resource = {
+                    uuid,
+                    name,
+                    description,
+                    kind: ContextMenuKind.COLLECTION
+                };
+                this.props.dispatch(
+                    contextMenuActions.OPEN_CONTEXT_MENU({
+                        position: { x: event.clientX, y: event.clientY },
+                        resource
+                    })
+                );
             }
 
-            componentWillReceiveProps({ match, item, onItemRouteChange }: CollectionPanelProps) {
-                if (!item || match.params.id !== item.uuid) {
-                    onItemRouteChange(match.params.id);
-                }
+            handleDelete = (uuid: string) => () => {
+                this.props.dispatch<any>(deleteCollectionTag(uuid));
+            }
+
+            onCopy = () => {
+                this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
+                    message: "Uuid has been copied",
+                    hideDuration: 2000
+                }));
             }
 
         }
     )
-);
\ No newline at end of file
+);
+
+const renderTagLabel = (tag: TagResource) => {
+    const { properties } = tag;
+    return `${properties.key}: ${properties.value}`;
+};