X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/6e44781d01db889030cc5f7819aa7f15fe837e19..c11055f2d6ce8385088bc221eab1175e31777ec0:/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 d9e1f803..68eeb3c1 100644 --- a/src/views-components/data-explorer/data-explorer.tsx +++ b/src/views-components/data-explorer/data-explorer.tsx @@ -2,229 +2,68 @@ // // SPDX-License-Identifier: AGPL-3.0 -import * as React from 'react'; -import { Grid, Paper, Toolbar, Typography } from '@material-ui/core'; -import IconButton from '@material-ui/core/IconButton'; -import MoreVertIcon from "@material-ui/icons/MoreVert"; -import { formatDate, formatFileSize } from '../../common/formatters'; -import { DataItem } from './data-item'; -import DataTable, { DataColumns } from "../../components/data-table/data-table"; -import ContextMenu from "../../components/context-menu/context-menu"; -import ColumnSelector from "../../components/column-selector/column-selector"; -import { mockAnchorFromMouseEvent } from "../../components/popover/helpers"; -import { DataColumn } from "../../components/data-table/data-column"; -import { ResourceKind } from "../../models/resource"; - -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; +import { connect } from "react-redux"; +import { RootState } from "~/store/store"; +import { DataExplorer as DataExplorerComponent } from "~/components/data-explorer/data-explorer"; +import { getDataExplorer } from "~/store/data-explorer/data-explorer-reducer"; +import { Dispatch } from "redux"; +import { dataExplorerActions } 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 { DataColumns } from "~/components/data-table/data-table"; + +interface Props { + id: string; + columns: DataColumns; + onRowClick: (item: any) => void; + onContextMenu: (event: React.MouseEvent, item: any) => void; + onRowDoubleClick: (item: any) => void; + extractKey?: (item: any) => React.Key; } -interface DataExplorerState { - columns: DataColumns; - contextMenu: { - anchorEl?: HTMLElement; - item?: DataItem; - }; -} +const mapStateToProps = (state: RootState, { id }: Props) => + getDataExplorer(state.dataExplorer, id); -class DataExplorer extends React.Component { - state: DataExplorerState = { - contextMenu: {}, - columns: [{ - name: "Name", - selected: true, - render: item => this.renderName(item) - }, { - name: "Status", - selected: true, - render: item => renderStatus(item.status) - }, { - name: "Type", - selected: true, - render: item => renderType(item.kind) - }, { - name: "Owner", - selected: true, - render: item => renderOwner(item.owner) - }, { - name: "File size", - selected: true, - render: item => renderFileSize(item.fileSize) - }, { - name: "Last modified", - selected: true, - 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") - } - ]]; +const mapDispatchToProps = () => { + let prevColumns: DataColumns; + return (dispatch: Dispatch, { id, columns, onRowClick, onRowDoubleClick, onContextMenu }: Props) => { + if (columns !== prevColumns) { + prevColumns = columns; + dispatch(dataExplorerActions.SET_COLUMNS({ id, columns })); + } + return { + onSearch: (searchValue: string) => { + dispatch(dataExplorerActions.SET_SEARCH_VALUE({ id, searchValue })); + }, - render() { - return - - - - - - - - - ; - } + onColumnToggle: (column: DataColumn) => { + dispatch(dataExplorerActions.TOGGLE_COLUMN({ id, columnName: column.name })); + }, - toggleColumn = (column: DataColumn) => { - 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 }); - } + onSortToggle: (column: DataColumn) => { + dispatch(dataExplorerActions.TOGGLE_SORT({ id, columnName: column.name })); + }, - renderName = (item: DataItem) => - this.props.onItemClick(item)}> - - {renderIcon(item)} - - - - {item.name} - - - + onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn) => { + dispatch(dataExplorerActions.SET_FILTERS({ id, columnName: column.name, filters })); + }, - renderActions = (item: DataItem) => - - this.openItemMenuOnActionsClick(event, item)}> - - - + onChangePage: (page: number) => { + dispatch(dataExplorerActions.SET_PAGE({ id, page })); + }, - openItemMenuOnRowClick = (event: React.MouseEvent, item: DataItem) => { - event.preventDefault(); - this.setState({ - contextMenu: { - anchorEl: mockAnchorFromMouseEvent(event), - item - } - }); - } + onChangeRowsPerPage: (rowsPerPage: number) => { + dispatch(dataExplorerActions.SET_ROWS_PER_PAGE({ id, rowsPerPage })); + }, - openItemMenuOnActionsClick = (event: React.MouseEvent, item: DataItem) => { - this.setState({ - contextMenu: { - anchorEl: event.currentTarget, - item - } - }); - } + onRowClick, - closeContextMenu = () => { - this.setState({ contextMenu: {} }); - } + onRowDoubleClick, - handleContextAction(action: keyof DataExplorerContextActions) { - return (item: DataItem) => { - this.closeContextMenu(); - this.props.contextActions[action](item); + onContextMenu, }; - } - -} - -const renderIcon = (dataItem: DataItem) => { - switch (dataItem.kind) { - case ResourceKind.LEVEL_UP: - return ; - case ResourceKind.PROJECT: - return ; - case ResourceKind.COLLECTION: - return ; - default: - return ; - } + }; }; -const renderDate = (date: string) => - - {formatDate(date)} - ; - -const renderFileSize = (fileSize?: number) => - - {formatFileSize(fileSize)} - ; - -const renderOwner = (owner: string) => - - {owner} - ; - -const renderType = (type: string) => - - {type} - ; - -const renderStatus = (status?: string) => - - {status || "-"} - ; +export const DataExplorer = connect(mapStateToProps, mapDispatchToProps())(DataExplorerComponent); -export default DataExplorer;