refs #13986 Merge branch 'origin/13986-projects-list-and-default-routing'
[arvados-workbench2.git] / src / views-components / data-explorer / data-explorer.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from "react-redux";
6 import { RootState } from "~/store/store";
7 import { DataExplorer as DataExplorerComponent } from "~/components/data-explorer/data-explorer";
8 import { getDataExplorer } from "~/store/data-explorer/data-explorer-reducer";
9 import { Dispatch } from "redux";
10 import { dataExplorerActions } from "~/store/data-explorer/data-explorer-action";
11 import { DataColumn } from "~/components/data-table/data-column";
12 import { DataTableFilterItem } from "~/components/data-table-filters/data-table-filters";
13 import { DataColumns } from "~/components/data-table/data-table";
14
15 interface Props {
16     id: string;
17     columns: DataColumns<any>;
18     onRowClick: (item: any) => void;
19     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: any) => void;
20     onRowDoubleClick: (item: any) => void;
21     extractKey?: (item: any) => React.Key;
22 }
23
24 const mapStateToProps = (state: RootState, { id, columns }: Props) => {
25     const s = getDataExplorer(state.dataExplorer, id);
26     if (s.columns.length === 0) {
27         s.columns = columns;
28     }
29     return s;
30 };
31
32 const mapDispatchToProps = () => {
33     return (dispatch: Dispatch, { id, columns, onRowClick, onRowDoubleClick, onContextMenu }: Props) => ({
34         onSetColumns: (columns: DataColumns<any>) => {
35             dispatch(dataExplorerActions.SET_COLUMNS({ id, columns }));
36         },
37
38         onSearch: (searchValue: string) => {
39             dispatch(dataExplorerActions.SET_SEARCH_VALUE({ id, searchValue }));
40         },
41
42         onColumnToggle: (column: DataColumn<any>) => {
43             dispatch(dataExplorerActions.TOGGLE_COLUMN({ id, columnName: column.name }));
44         },
45
46         onSortToggle: (column: DataColumn<any>) => {
47             dispatch(dataExplorerActions.TOGGLE_SORT({ id, columnName: column.name }));
48         },
49
50         onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<any>) => {
51             dispatch(dataExplorerActions.SET_FILTERS({ id, columnName: column.name, filters }));
52         },
53
54         onChangePage: (page: number) => {
55             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
56         },
57
58         onChangeRowsPerPage: (rowsPerPage: number) => {
59             dispatch(dataExplorerActions.SET_ROWS_PER_PAGE({ id, rowsPerPage }));
60         },
61
62         onRowClick,
63
64         onRowDoubleClick,
65
66         onContextMenu,
67     });
68 };
69
70 export const DataExplorer = connect(mapStateToProps, mapDispatchToProps())(DataExplorerComponent);
71