//
// SPDX-License-Identifier: AGPL-3.0
-import * as React from 'react';
-import { Typography, Grid, ListItem, Divider, List, ListItemIcon, ListItemText, Paper, Toolbar } from '@material-ui/core';
-import IconButton, { IconButtonProps } from '@material-ui/core/IconButton';
-import MoreVertIcon from "@material-ui/icons/MoreVert";
-import Popover from '../../components/popover/popover';
-import { formatFileSize, formatDate } from '../../common/formatters';
-import { DataItem } from './data-item';
-import { DataColumns, DataTableProps } from "../../components/data-table/data-table";
-import { DataColumn } from "../../components/data-table/data-column";
-import ColumnSelector from "../../components/column-selector/column-selector";
-import DataTable from "../../components/data-table/data-table";
-
-interface DataExplorerProps {
- items: DataItem[];
- onItemClick: (item: DataItem) => void;
-}
-
-interface DataExplorerState {
- columns: DataColumns<DataItem>;
+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<any>;
+ onRowClick: (item: any) => void;
+ onContextMenu: (event: React.MouseEvent<HTMLElement>, item: any) => void;
+ onRowDoubleClick: (item: any) => void;
+ extractKey?: (item: any) => React.Key;
}
-class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState> {
- state: DataExplorerState = {
- 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.type)
+const mapStateToProps = (state: RootState, { id }: Props) =>
+ getDataExplorer(state.dataExplorer, id);
+
+const mapDispatchToProps = () => {
+ let prevColumns: DataColumns<any>;
+ 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 }));
},
- {
- name: "Owner",
- selected: true,
- render: item => renderOwner(item.owner)
+
+ onColumnToggle: (column: DataColumn<any>) => {
+ dispatch(dataExplorerActions.TOGGLE_COLUMN({ id, columnName: column.name }));
},
- {
- name: "File size",
- selected: true,
- render: (item) => renderFileSize(item.fileSize)
+
+ onSortToggle: (column: DataColumn<any>) => {
+ dispatch(dataExplorerActions.TOGGLE_SORT({ id, columnName: column.name }));
},
- {
- name: "Last modified",
- selected: true,
- render: item => renderDate(item.lastModified)
+
+ onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<any>) => {
+ dispatch(dataExplorerActions.SET_FILTERS({ id, columnName: column.name, filters }));
},
- {
- name: "Actions",
- selected: true,
- configurable: false,
- renderHeader: () => null,
- render: renderItemActions
- }
- ]
- };
- render() {
- return <Paper>
- <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} />
- <Toolbar />
- </Paper>;
- }
+ onChangePage: (page: number) => {
+ dispatch(dataExplorerActions.SET_PAGE({ id, page }));
+ },
- 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 });
- }
+ onChangeRowsPerPage: (rowsPerPage: number) => {
+ dispatch(dataExplorerActions.SET_ROWS_PER_PAGE({ id, rowsPerPage }));
+ },
- 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>
+ onRowClick,
-}
+ onRowDoubleClick,
-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 />;
- }
+ onContextMenu,
+ };
+ };
};
-const renderDate = (date: string) =>
- <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>
- {type}
- </Typography>;
-
-const renderStatus = (status?: string) =>
- <Typography noWrap align="center">
- {status || "-"}
- </Typography>;
-
-const renderItemActions = () =>
- <Grid container justify="flex-end">
- <Popover triggerComponent={ItemActionsTrigger}>
- <List dense>
- {[{
- icon: "fas fa-users",
- label: "Share"
- },
- {
- icon: "fas fa-sign-out-alt",
- label: "Move to"
- },
- {
- icon: "fas fa-star",
- label: "Add to favourite"
- },
- {
- icon: "fas fa-edit",
- label: "Rename"
- },
- {
- icon: "fas fa-copy",
- label: "Make a copy"
- },
- {
- icon: "fas fa-download",
- label: "Download"
- }].map(renderAction)}
- < Divider />
- {renderAction({ icon: "fas fa-trash-alt", label: "Remove" })}
- </List>
- </Popover>
- </Grid>;
-
-const renderAction = (action: { label: string, icon: string }, index?: number) =>
- <ListItem button key={index}>
- <ListItemIcon>
- <i className={action.icon} />
- </ListItemIcon>
- <ListItemText>
- {action.label}
- </ListItemText>
- </ListItem>;
-
-const ItemActionsTrigger: React.SFC<IconButtonProps> = (props) =>
- <IconButton {...props}>
- <MoreVertIcon />
- </IconButton>;
+export const DataExplorer = connect(mapStateToProps, mapDispatchToProps())(DataExplorerComponent);
-export default DataExplorer;