X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/1a9eb2261e6030ba78078e2a206bad27653f2475..1933452553965a2066191eccaaa40943d6d5c5d8:/src/views-components/data-explorer/data-explorer.tsx

diff --git a/src/views-components/data-explorer/data-explorer.tsx b/src/views-components/data-explorer/data-explorer.tsx
index ffb21f93fb..2864cefe17 100644
--- a/src/views-components/data-explorer/data-explorer.tsx
+++ b/src/views-components/data-explorer/data-explorer.tsx
@@ -2,236 +2,54 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from 'react';
-import { Typography, Grid, Paper, Toolbar } from '@material-ui/core';
-import IconButton from '@material-ui/core/IconButton';
-import MoreVertIcon from "@material-ui/icons/MoreVert";
-import { formatFileSize, formatDate } from '../../common/formatters';
-import { DataItem } from './data-item';
-import ContextMenu from "../../components/context-menu/context-menu";
-import ColumnSelector from "../../components/column-selector/column-selector";
-import DataTable from "../../components/data-table/data-table";
-import { mockAnchorFromMouseEvent } from "../../components/popover/helpers";
-import { DataColumn, toggleSortDirection, resetSortDirection } from "../../components/data-table/data-column";
-
-export interface DataExplorerContextActions {
-    onAddToFavourite: (dataIitem: DataItem) => void;
-    onCopy: (dataIitem: DataItem) => void;
-    onDownload: (dataIitem: DataItem) => void;
-    onMoveTo: (dataIitem: DataItem) => void;
-    onRemove: (dataIitem: DataItem) => void;
-    onRename: (dataIitem: DataItem) => void;
-    onShare: (dataIitem: DataItem) => void;
-}
-interface DataExplorerProps {
-    items: DataItem[];
-    onItemClick: (item: DataItem) => void;
-    contextActions: DataExplorerContextActions;
-}
-
-interface DataExplorerState {
-    columns: Array<DataColumn<DataItem>>;
-    contextMenu: {
-        anchorEl?: HTMLElement;
-        item?: DataItem;
-    };
+import { connect } from "react-redux";
+import { RootState } from "../../store/store";
+import DataExplorer from "../../components/data-explorer/data-explorer";
+import { getDataExplorer } from "../../store/data-explorer/data-explorer-reducer";
+import { Dispatch } from "redux";
+import actions from "../../store/data-explorer/data-explorer-action";
+import { DataColumn } from "../../components/data-table/data-column";
+import { DataTableFilterItem } from "../../components/data-table-filters/data-table-filters";
+import { ContextMenuAction, ContextMenuActionGroup } from "../../components/context-menu/context-menu";
+
+interface Props {
+    id: string;
+    onRowClick: (item: any) => void;
+    onContextMenu: (event: React.MouseEvent<HTMLElement>, item: any) => void;
 }
 
-class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState> {
-    state: DataExplorerState = {
-        contextMenu: {},
-        columns: [{
-            name: "Name",
-            selected: true,
-            sortDirection: "asc",
-            onSortToggle: () => this.toggleSort("Name"),
-            render: item => this.renderName(item)
-        }, {
-            name: "Status",
-            selected: true,
-            render: item => renderStatus(item.status)
-        }, {
-            name: "Type",
-            selected: true,
-            render: item => renderType(item.type)
-        }, {
-            name: "Owner",
-            selected: true,
-            render: item => renderOwner(item.owner)
-        }, {
-            name: "File size",
-            selected: true,
-            render: item => renderFileSize(item.fileSize)
-        }, {
-            name: "Last modified",
-            selected: true,
-            onSortToggle: () => this.toggleSort("Last modified"),
-            render: item => renderDate(item.lastModified)
-        }, {
-            name: "Actions",
-            selected: true,
-            configurable: false,
-            renderHeader: () => null,
-            render: item => this.renderActions(item)
-        }]
-    };
-
-    contextMenuActions = [[{
-        icon: "fas fa-users fa-fw",
-        name: "Share",
-        onClick: this.handleContextAction("onShare")
-    }, {
-        icon: "fas fa-sign-out-alt fa-fw",
-        name: "Move to",
-        onClick: this.handleContextAction("onMoveTo")
-    }, {
-        icon: "fas fa-star fa-fw",
-        name: "Add to favourite",
-        onClick: this.handleContextAction("onAddToFavourite")
-    }, {
-        icon: "fas fa-edit fa-fw",
-        name: "Rename",
-        onClick: this.handleContextAction("onRename")
-    }, {
-        icon: "fas fa-copy fa-fw",
-        name: "Make a copy",
-        onClick: this.handleContextAction("onCopy")
-    }, {
-        icon: "fas fa-download fa-fw",
-        name: "Download",
-        onClick: this.handleContextAction("onDownload")
-    }], [{
-        icon: "fas fa-trash-alt fa-fw",
-        name: "Remove",
-        onClick: this.handleContextAction("onRemove")
-    }
-    ]];
-
-    render() {
-        return <Paper>
-            <ContextMenu
-                {...this.state.contextMenu}
-                actions={this.contextMenuActions}
-                onClose={this.closeContextMenu} />
-            <Toolbar>
-                <Grid container justify="flex-end">
-                    <ColumnSelector
-                        columns={this.state.columns}
-                        onColumnToggle={this.toggleColumn} />
-                </Grid>
-            </Toolbar>
-            <DataTable
-                columns={this.state.columns}
-                items={this.props.items}
-                onRowContextMenu={this.openItemMenuOnRowClick} />
-            <Toolbar />
-        </Paper>;
-    }
-
-    toggleColumn = (column: DataColumn<DataItem>) => {
-        const index = this.state.columns.indexOf(column);
-        const columns = this.state.columns.slice(0);
-        columns.splice(index, 1, { ...column, selected: !column.selected });
-        this.setState({ columns });
-    }
+const mapStateToProps = (state: RootState, { id }: Props) =>
+    getDataExplorer(state.dataExplorer, id);
 
-    renderName = (item: DataItem) =>
-        <Grid
-            container
-            alignItems="center"
-            wrap="nowrap"
-            spacing={16}
-            onClick={() => this.props.onItemClick(item)}>
-            <Grid item>
-                {renderIcon(item)}
-            </Grid>
-            <Grid item>
-                <Typography color="primary">
-                    {item.name}
-                </Typography>
-            </Grid>
-        </Grid>
+const mapDispatchToProps = (dispatch: Dispatch, { id, onRowClick, onContextMenu }: Props) => ({
+    onSearch: (searchValue: string) => {
+        dispatch(actions.SET_SEARCH_VALUE({ id, searchValue }));
+    },
 
-    renderActions = (item: DataItem) =>
-        <Grid container justify="flex-end">
-            <IconButton onClick={event => this.openItemMenuOnActionsClick(event, item)}>
-                <MoreVertIcon />
-            </IconButton>
-        </Grid>
+    onColumnToggle: (column: DataColumn<any>) => {
+        dispatch(actions.TOGGLE_COLUMN({ id, columnName: column.name }));
+    },
 
-    openItemMenuOnRowClick = (event: React.MouseEvent<HTMLElement>, item: DataItem) => {
-        event.preventDefault();
-        this.setState({
-            contextMenu: {
-                anchorEl: mockAnchorFromMouseEvent(event),
-                item
-            }
-        });
-    }
-
-    openItemMenuOnActionsClick = (event: React.MouseEvent<HTMLElement>, item: DataItem) => {
-        this.setState({
-            contextMenu: {
-                anchorEl: event.currentTarget,
-                item
-            }
-        });
-    }
-
-    closeContextMenu = () => {
-        this.setState({ contextMenu: {} });
-    }
-
-    handleContextAction(action: keyof DataExplorerContextActions) {
-        return (item: DataItem) => {
-            this.closeContextMenu();
-            this.props.contextActions[action](item);
-        };
-    }
-
-    toggleSort = (columnName: string) => {
-        this.setState({
-            columns: this.state.columns.map((column, index) =>
-                column.name === columnName ? toggleSortDirection(column) : resetSortDirection(column))
-        });
-    }
-
-}
+    onSortToggle: (column: DataColumn<any>) => {
+        dispatch(actions.TOGGLE_SORT({ id, columnName: column.name }));
+    },
 
-const renderIcon = (dataItem: DataItem) => {
-    switch (dataItem.type) {
-        case "arvados#group":
-            return <i className="fas fa-folder fa-lg" />;
-        case "arvados#groupList":
-            return <i className="fas fa-th fa-lg" />;
-        default:
-            return <i />;
-    }
-};
+    onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<any>) => {
+        dispatch(actions.SET_FILTERS({ id, columnName: column.name, filters }));
+    },
 
-const renderDate = (date: string) =>
-    <Typography noWrap>
-        {formatDate(date)}
-    </Typography>;
+    onChangePage: (page: number) => {
+        dispatch(actions.SET_PAGE({ id, page }));
+    },
 
-const renderFileSize = (fileSize?: number) =>
-    <Typography noWrap>
-        {formatFileSize(fileSize)}
-    </Typography>;
+    onChangeRowsPerPage: (rowsPerPage: number) => {
+        dispatch(actions.SET_ROWS_PER_PAGE({ id, rowsPerPage }));
+    },
 
-const renderOwner = (owner: string) =>
-    <Typography noWrap color="primary">
-        {owner}
-    </Typography>;
+    onRowClick,
 
-const renderType = (type: string) =>
-    <Typography noWrap>
-        {type}
-    </Typography>;
+    onContextMenu
+});
 
-const renderStatus = (status?: string) =>
-    <Typography noWrap align="center">
-        {status || "-"}
-    </Typography>;
+export default connect(mapStateToProps, mapDispatchToProps)(DataExplorer);
 
-export default DataExplorer;
