Add toolbar with project explorer actions
[arvados-workbench2.git] / src / views-components / project-explorer / project-explorer.tsx
index 4931c09a5149b67c1b3f5b300a6bf3b8798da093..16f670cb008d76b8d4cc3baaebe4ef57df32d843 100644 (file)
 
 import * as React from 'react';
 import { ProjectExplorerItem } from './project-explorer-item';
-import { Grid, Typography } from '@material-ui/core';
+import { Grid, Typography, Button, Toolbar, StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
 import { formatDate, formatFileSize } from '../../common/formatters';
-import DataExplorer from '../../components/data-explorer/data-explorer';
-import { DataColumn, toggleSortDirection, resetSortDirection } from '../../components/data-table/data-column';
+import DataExplorer from '../data-explorer/data-explorer';
+import { DataColumn } from '../../components/data-table/data-column';
 import { DataTableFilterItem } from '../../components/data-table-filters/data-table-filters';
 import { ContextMenuAction } from '../../components/context-menu/context-menu';
+import { DispatchProp, connect } from 'react-redux';
+import actions from "../../store/data-explorer/data-explorer-action";
+import { DataColumns } from '../../components/data-table/data-table';
 
-export interface ProjectExplorerContextActions {
-    onAddToFavourite: (item: ProjectExplorerItem) => void;
-    onCopy: (item: ProjectExplorerItem) => void;
-    onDownload: (item: ProjectExplorerItem) => void;
-    onMoveTo: (item: ProjectExplorerItem) => void;
-    onRemove: (item: ProjectExplorerItem) => void;
-    onRename: (item: ProjectExplorerItem) => void;
-    onShare: (item: ProjectExplorerItem) => void;
-}
-
-interface ProjectExplorerProps {
-    items: ProjectExplorerItem[];
-}
-
-interface ProjectExplorerState {
-    columns: Array<DataColumn<ProjectExplorerItem>>;
-    searchValue: string;
-    page: number;
-    rowsPerPage: number;
-}
-
-class ProjectExplorer extends React.Component<ProjectExplorerProps, ProjectExplorerState> {
-    state: ProjectExplorerState = {
-        searchValue: "",
-        page: 0,
-        rowsPerPage: 10,
-        columns: [{
-            name: "Name",
-            selected: true,
-            sortDirection: "asc",
-            render: renderName
-        }, {
-            name: "Status",
-            selected: true,
-            filters: [{
-                name: "In progress",
-                selected: true
-            }, {
-                name: "Complete",
-                selected: true
-            }],
-            render: renderStatus
-        }, {
-            name: "Type",
-            selected: true,
-            filters: [{
-                name: "Collection",
-                selected: true
-            }, {
-                name: "Group",
-                selected: true
-            }],
-            render: item => renderType(item.type)
-        }, {
-            name: "Owner",
-            selected: true,
-            render: item => renderOwner(item.owner)
-        }, {
-            name: "File size",
-            selected: true,
-            sortDirection: "none",
-            render: item => renderFileSize(item.fileSize)
-        }, {
-            name: "Last modified",
-            selected: true,
-            render: item => renderDate(item.lastModified)
-        }]
-    };
-
-    contextMenuActions = [[{
-        icon: "fas fa-users fa-fw",
-        name: "Share"
-    }, {
-        icon: "fas fa-sign-out-alt fa-fw",
-        name: "Move to"
-    }, {
-        icon: "fas fa-star fa-fw",
-        name: "Add to favourite"
-    }, {
-        icon: "fas fa-edit fa-fw",
-        name: "Rename"
-    }, {
-        icon: "fas fa-copy fa-fw",
-        name: "Make a copy"
-    }, {
-        icon: "fas fa-download fa-fw",
-        name: "Download"
-    }], [{
-        icon: "fas fa-trash-alt fa-fw",
-        name: "Remove"
+export const PROJECT_EXPLORER_ID = "projectExplorer";
+class ProjectExplorer extends React.Component<DispatchProp & WithStyles<CssRules>> {
+    render() {
+        return <div>
+            <div className={this.props.classes.toolbar}>
+                <Button color="primary" variant="raised" className={this.props.classes.button}>
+                    Create a collection
+                </Button>
+                <Button color="primary" variant="raised" className={this.props.classes.button}>
+                    Run a process
+                </Button>
+                <Button color="primary" variant="raised" className={this.props.classes.button}>
+                    Create a project
+                </Button>
+            </div>
+            <DataExplorer
+                id={PROJECT_EXPLORER_ID}
+                contextActions={contextMenuActions}
+                onColumnToggle={this.toggleColumn}
+                onFiltersChange={this.changeFilters}
+                onRowClick={console.log}
+                onSortToggle={this.toggleSort}
+                onSearch={this.search}
+                onContextAction={this.executeAction}
+                onChangePage={this.changePage}
+                onChangeRowsPerPage={this.changeRowsPerPage} />;
+        </div>;
     }
-    ]];
 
-    render() {
-        return <DataExplorer
-            items={this.props.items}
-            columns={this.state.columns}
-            contextActions={this.contextMenuActions}
-            searchValue={this.state.searchValue}
-            page={this.state.page}
-            rowsPerPage={this.state.rowsPerPage}
-            onColumnToggle={this.toggleColumn}
-            onFiltersChange={this.changeFilters}
-            onRowClick={console.log}
-            onSortToggle={this.toggleSort}
-            onSearch={this.search}
-            onContextAction={this.executeAction}
-            onChangePage={this.changePage}
-            onChangeRowsPerPage={this.changeRowsPerPage} />;
+    componentDidMount() {
+        this.props.dispatch(actions.SET_COLUMNS({ id: PROJECT_EXPLORER_ID, columns }));
     }
 
     toggleColumn = (toggledColumn: DataColumn<ProjectExplorerItem>) => {
-        this.setState({
-            columns: this.state.columns.map(column =>
-                column.name === toggledColumn.name
-                    ? { ...column, selected: !column.selected }
-                    : column
-            )
-        });
+        this.props.dispatch(actions.TOGGLE_COLUMN({ id: PROJECT_EXPLORER_ID, columnName: toggledColumn.name }));
     }
 
     toggleSort = (toggledColumn: DataColumn<ProjectExplorerItem>) => {
-        this.setState({
-            columns: this.state.columns.map(column =>
-                column.name === toggledColumn.name
-                    ? toggleSortDirection(column)
-                    : resetSortDirection(column)
-            )
-        });
+        this.props.dispatch(actions.TOGGLE_SORT({ id: PROJECT_EXPLORER_ID, columnName: toggledColumn.name }));
     }
 
     changeFilters = (filters: DataTableFilterItem[], updatedColumn: DataColumn<ProjectExplorerItem>) => {
-        this.setState({
-            columns: this.state.columns.map(column =>
-                column.name === updatedColumn.name
-                    ? { ...column, filters }
-                    : column
-            )
-        });
+        this.props.dispatch(actions.SET_FILTERS({ id: PROJECT_EXPLORER_ID, columnName: updatedColumn.name, filters }));
     }
 
     executeAction = (action: ContextMenuAction, item: ProjectExplorerItem) => {
@@ -157,18 +64,30 @@ class ProjectExplorer extends React.Component<ProjectExplorerProps, ProjectExplo
     }
 
     search = (searchValue: string) => {
-        this.setState({ searchValue });
+        this.props.dispatch(actions.SET_SEARCH_VALUE({ id: PROJECT_EXPLORER_ID, searchValue }));
     }
 
     changePage = (page: number) => {
-        this.setState({ page });
+        this.props.dispatch(actions.SET_PAGE({ id: PROJECT_EXPLORER_ID, page }));
     }
 
     changeRowsPerPage = (rowsPerPage: number) => {
-        this.setState({ rowsPerPage });
+        this.props.dispatch(actions.SET_ROWS_PER_PAGE({ id: PROJECT_EXPLORER_ID, rowsPerPage }));
     }
 }
 
+type CssRules = "toolbar" | "button";
+
+const styles: StyleRulesCallback<CssRules> = theme => ({
+    toolbar: {
+        paddingBottom: theme.spacing.unit * 3,
+        textAlign: "right"
+    },
+    button: {
+        marginLeft: theme.spacing.unit
+    }
+});
+
 const renderName = (item: ProjectExplorerItem) =>
     <Grid
         container
@@ -221,4 +140,70 @@ const renderStatus = (item: ProjectExplorerItem) =>
         {item.status || "-"}
     </Typography>;
 
-export default ProjectExplorer;
+const columns: DataColumns<ProjectExplorerItem> = [{
+    name: "Name",
+    selected: true,
+    sortDirection: "asc",
+    render: renderName
+}, {
+    name: "Status",
+    selected: true,
+    filters: [{
+        name: "In progress",
+        selected: true
+    }, {
+        name: "Complete",
+        selected: true
+    }],
+    render: renderStatus
+}, {
+    name: "Type",
+    selected: true,
+    filters: [{
+        name: "Collection",
+        selected: true
+    }, {
+        name: "Group",
+        selected: true
+    }],
+    render: item => renderType(item.type)
+}, {
+    name: "Owner",
+    selected: true,
+    render: item => renderOwner(item.owner)
+}, {
+    name: "File size",
+    selected: true,
+    sortDirection: "none",
+    render: item => renderFileSize(item.fileSize)
+}, {
+    name: "Last modified",
+    selected: true,
+    render: item => renderDate(item.lastModified)
+}];
+
+const contextMenuActions = [[{
+    icon: "fas fa-users fa-fw",
+    name: "Share"
+}, {
+    icon: "fas fa-sign-out-alt fa-fw",
+    name: "Move to"
+}, {
+    icon: "fas fa-star fa-fw",
+    name: "Add to favourite"
+}, {
+    icon: "fas fa-edit fa-fw",
+    name: "Rename"
+}, {
+    icon: "fas fa-copy fa-fw",
+    name: "Make a copy"
+}, {
+    icon: "fas fa-download fa-fw",
+    name: "Download"
+}], [{
+    icon: "fas fa-trash-alt fa-fw",
+    name: "Remove"
+}
+]];
+
+export default withStyles(styles)(connect()(ProjectExplorer));