Refactor to apply global navigation actions
[arvados-workbench2.git] / src / views / project-panel / project-panel.tsx
index c2b42a55deb4de79b35abae49b2c333e0e69cb30..37712c7dcb47f3184bf9a72c8669cd964aa85167 100644 (file)
@@ -3,26 +3,43 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { ProjectPanelItem } from './project-panel-item';
-import { Grid, Typography, Button, StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
-import { formatDate, formatFileSize } from '../../common/formatters';
-import { DataExplorer } from "../../views-components/data-explorer/data-explorer";
+import { Button, StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
+import { DataExplorer } from "~/views-components/data-explorer/data-explorer";
 import { DispatchProp, connect } from 'react-redux';
-import { DataColumns } from '../../components/data-table/data-table';
+import { DataColumns } from '~/components/data-table/data-table';
 import { RouteComponentProps } from 'react-router';
-import { RootState } from '../../store/store';
-import { DataTableFilterItem } from '../../components/data-table-filters/data-table-filters';
-import { ContainerRequestState } from '../../models/container-request';
-import { SortDirection } from '../../components/data-table/data-column';
-import { ResourceKind } from '../../models/resource';
-import { resourceLabel } from '../../common/labels';
-import { ProjectIcon, CollectionIcon, ProcessIcon, DefaultIcon, FavoriteIcon } from '../../components/icon/icon';
-import { ArvadosTheme } from '../../common/custom-theme';
-import { FavoriteStar } from '../../views-components/favorite-star/favorite-star';
-
-type CssRules = "toolbar" | "button";
+import { RootState } from '~/store/store';
+import { DataTableFilterItem } from '~/components/data-table-filters/data-table-filters';
+import { ContainerRequestState } from '~/models/container-request';
+import { SortDirection } from '~/components/data-table/data-column';
+import { ResourceKind } from '~/models/resource';
+import { resourceLabel } from '~/common/labels';
+import { ArvadosTheme } from '~/common/custom-theme';
+import { ResourceFileSize, ResourceLastModifiedDate, ProcessStatus, ResourceType, ResourceOwner } from '~/views-components/data-explorer/renderers';
+import { ProjectIcon } from '~/components/icon/icon';
+import { ResourceName } from '~/views-components/data-explorer/renderers';
+import { ResourcesState, getResource } from '~/store/resources/resources';
+import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
+import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
+import { contextMenuActions } from '~/store/context-menu/context-menu-actions';
+import { CollectionResource } from '~/models/collection';
+import { ProjectResource } from '~/models/project';
+import { openProjectCreator } from '~/store/project/project-action';
+import { reset } from 'redux-form';
+import { COLLECTION_CREATE_DIALOG } from '~/views-components/dialog-create/dialog-collection-create';
+import { collectionCreateActions } from '~/store/collections/creator/collection-creator-action';
+import { navigateToResource } from '~/store/navigation/navigation-action';
+import { getProperty } from '~/store/properties/properties';
+import { PROJECT_PANEL_CURRENT_UUID } from '~/store/project-panel/project-panel-action';
+
+type CssRules = 'root' | "toolbar" | "button";
 
 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
+    root: {
+        position: 'relative',
+        width: '100%',
+        height: '100%'
+    },
     toolbar: {
         paddingBottom: theme.spacing.unit * 3,
         textAlign: "right"
@@ -32,61 +49,6 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     },
 });
 
-const renderName = (item: ProjectPanelItem) =>
-    <Grid container alignItems="center" wrap="nowrap" spacing={16}>
-        <Grid item>
-            {renderIcon(item)}
-        </Grid>
-        <Grid item>
-            <Typography color="primary">
-                {item.name}
-            </Typography>
-        </Grid>
-        <Grid item>
-            <Typography variant="caption">
-                <FavoriteStar resourceUuid={item.uuid} />
-            </Typography>
-        </Grid>
-    </Grid>;
-
-
-const renderIcon = (item: ProjectPanelItem) => {
-    switch (item.kind) {
-        case ResourceKind.Project:
-            return <ProjectIcon />;
-        case ResourceKind.Collection:
-            return <CollectionIcon />;
-        case ResourceKind.Process:
-            return <ProcessIcon />;
-        default:
-            return <DefaultIcon />;
-    }
-};
-
-const renderDate = (date: string) => {
-    return <Typography noWrap>{formatDate(date)}</Typography>;
-};
-
-const renderFileSize = (fileSize?: number) =>
-    <Typography noWrap>
-        {formatFileSize(fileSize)}
-    </Typography>;
-
-const renderOwner = (owner: string) =>
-    <Typography noWrap color="primary" >
-        {owner}
-    </Typography>;
-
-const renderType = (type: string) =>
-    <Typography noWrap>
-        {resourceLabel(type)}
-    </Typography>;
-
-const renderStatus = (item: ProjectPanelItem) =>
-    <Typography noWrap align="center" >
-        {item.status || "-"}
-    </Typography>;
-
 export enum ProjectPanelColumnNames {
     NAME = "Name",
     STATUS = "Status",
@@ -100,77 +62,91 @@ export interface ProjectPanelFilter extends DataTableFilterItem {
     type: ResourceKind | ContainerRequestState;
 }
 
-export const columns: DataColumns<ProjectPanelItem, ProjectPanelFilter> = [
+export const projectPanelColumns: DataColumns<string, ProjectPanelFilter> = [
     {
         name: ProjectPanelColumnNames.NAME,
         selected: true,
-        sortDirection: SortDirection.Asc,
-        render: renderName,
+        configurable: true,
+        sortDirection: SortDirection.ASC,
+        filters: [],
+        render: uuid => <ResourceName uuid={uuid} />,
         width: "450px"
     },
     {
         name: "Status",
         selected: true,
+        configurable: true,
+        sortDirection: SortDirection.NONE,
         filters: [
             {
-                name: ContainerRequestState.Committed,
+                name: ContainerRequestState.COMMITTED,
                 selected: true,
-                type: ContainerRequestState.Committed
+                type: ContainerRequestState.COMMITTED
             },
             {
-                name: ContainerRequestState.Final,
+                name: ContainerRequestState.FINAL,
                 selected: true,
-                type: ContainerRequestState.Final
+                type: ContainerRequestState.FINAL
             },
             {
-                name: ContainerRequestState.Uncommitted,
+                name: ContainerRequestState.UNCOMMITTED,
                 selected: true,
-                type: ContainerRequestState.Uncommitted
+                type: ContainerRequestState.UNCOMMITTED
             }
         ],
-        render: renderStatus,
+        render: uuid => <ProcessStatus uuid={uuid} />,
         width: "75px"
     },
     {
         name: ProjectPanelColumnNames.TYPE,
         selected: true,
+        configurable: true,
+        sortDirection: SortDirection.NONE,
         filters: [
             {
-                name: resourceLabel(ResourceKind.Collection),
+                name: resourceLabel(ResourceKind.COLLECTION),
                 selected: true,
-                type: ResourceKind.Collection
+                type: ResourceKind.COLLECTION
             },
             {
-                name: resourceLabel(ResourceKind.Process),
+                name: resourceLabel(ResourceKind.PROCESS),
                 selected: true,
-                type: ResourceKind.Process
+                type: ResourceKind.PROCESS
             },
             {
-                name: resourceLabel(ResourceKind.Project),
+                name: resourceLabel(ResourceKind.PROJECT),
                 selected: true,
-                type: ResourceKind.Project
+                type: ResourceKind.PROJECT
             }
         ],
-        render: item => renderType(item.kind),
+        render: uuid => <ResourceType uuid={uuid} />,
         width: "125px"
     },
     {
         name: ProjectPanelColumnNames.OWNER,
         selected: true,
-        render: item => renderOwner(item.owner),
+        configurable: true,
+        sortDirection: SortDirection.NONE,
+        filters: [],
+        render: uuid => <ResourceOwner uuid={uuid} />,
         width: "200px"
     },
     {
         name: ProjectPanelColumnNames.FILE_SIZE,
         selected: true,
-        render: item => renderFileSize(item.fileSize),
+        configurable: true,
+        sortDirection: SortDirection.NONE,
+        filters: [],
+        render: uuid => <ResourceFileSize uuid={uuid} />,
         width: "50px"
     },
     {
         name: ProjectPanelColumnNames.LAST_MODIFIED,
         selected: true,
-        sortDirection: SortDirection.None,
-        render: item => renderDate(item.lastModified),
+        configurable: true,
+        sortDirection: SortDirection.NONE,
+        filters: [],
+        render: uuid => <ResourceLastModifiedDate uuid={uuid} />,
         width: "150px"
     }
 ];
@@ -179,27 +155,23 @@ export const PROJECT_PANEL_ID = "projectPanel";
 
 interface ProjectPanelDataProps {
     currentItemId: string;
+    resources: ResourcesState;
 }
 
-interface ProjectPanelActionProps {
-    onItemClick: (item: ProjectPanelItem) => void;
-    onContextMenu: (event: React.MouseEvent<HTMLElement>, item: ProjectPanelItem) => void;
-    onDialogOpen: (ownerUuid: string) => void;
-    onItemDoubleClick: (item: ProjectPanelItem) => void;
-    onItemRouteChange: (itemId: string) => void;
-}
-
-type ProjectPanelProps = ProjectPanelDataProps & ProjectPanelActionProps & DispatchProp
+type ProjectPanelProps = ProjectPanelDataProps & DispatchProp
     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
 
 export const ProjectPanel = withStyles(styles)(
-    connect((state: RootState) => ({ currentItemId: state.projects.currentItemId }))(
+    connect((state: RootState) => ({
+        currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
+        resources: state.resources
+    }))(
         class extends React.Component<ProjectPanelProps> {
             render() {
                 const { classes } = this.props;
-                return <div>
+                return <div className={classes.root}>
                     <div className={classes.toolbar}>
-                        <Button color="primary" variant="raised" className={classes.button}>
+                        <Button color="primary" onClick={this.handleNewCollectionClick} variant="raised" className={classes.button}>
                             Create a collection
                         </Button>
                         <Button color="primary" variant="raised" className={classes.button}>
@@ -211,21 +183,60 @@ export const ProjectPanel = withStyles(styles)(
                     </div>
                     <DataExplorer
                         id={PROJECT_PANEL_ID}
-                        onRowClick={this.props.onItemClick}
-                        onRowDoubleClick={this.props.onItemDoubleClick}
-                        onContextMenu={this.props.onContextMenu}
-                        extractKey={(item: ProjectPanelItem) => item.uuid} />
+                        onRowClick={this.handleRowClick}
+                        onRowDoubleClick={this.handleRowDoubleClick}
+                        onContextMenu={this.handleContextMenu}
+                        defaultIcon={ProjectIcon}
+                        defaultMessages={['Your project is empty.', 'Please create a project or create a collection and upload a data.']} />
                 </div>;
             }
 
             handleNewProjectClick = () => {
-                this.props.onDialogOpen(this.props.currentItemId);
+                this.props.dispatch<any>(openProjectCreator(this.props.currentItemId));
+            }
+
+            handleNewCollectionClick = () => {
+                this.props.dispatch(reset(COLLECTION_CREATE_DIALOG));
+                this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: this.props.currentItemId }));
             }
-            componentWillReceiveProps({ match, currentItemId, onItemRouteChange }: ProjectPanelProps) {
-                if (match.params.id !== currentItemId) {
-                    onItemRouteChange(match.params.id);
+
+            handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
+                event.preventDefault();
+                const resource = getResource(resourceUuid)(this.props.resources) as CollectionResource | ProjectResource | undefined;
+                if (resource) {
+                    let kind: ContextMenuKind;
+
+                    if (resource.kind === ResourceKind.PROJECT) {
+                        kind = ContextMenuKind.PROJECT;
+                    } else if (resource.kind === ResourceKind.COLLECTION) {
+                        kind = ContextMenuKind.COLLECTION_RESOURCE;
+                    } else {
+                        kind = ContextMenuKind.RESOURCE;
+                    }
+                    if (kind !== ContextMenuKind.RESOURCE) {
+                        this.props.dispatch(
+                            contextMenuActions.OPEN_CONTEXT_MENU({
+                                position: { x: event.clientX, y: event.clientY },
+                                resource: {
+                                    uuid: resource.uuid,
+                                    name: resource.name || '',
+                                    description: resource.description,
+                                    kind,
+                                }
+                            })
+                        );
+                    }
                 }
             }
+
+            handleRowDoubleClick = (uuid: string) => {
+                this.props.dispatch<any>(navigateToResource(uuid));
+            }
+
+            handleRowClick = (uuid: string) => {
+                this.props.dispatch(loadDetailsPanel(uuid));
+            }
+
         }
     )
-);
\ No newline at end of file
+);