Improve pagination actions in data explorer
[arvados-workbench2.git] / src / store / data-explorer / data-explorer-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { DataColumn, toggleSortDirection, resetSortDirection } from "../../components/data-table/data-column";
6 import actions, { DataExplorerAction } from "./data-explorer-action";
7 import { DataTableFilterItem } from "../../components/data-table-filters/data-table-filters";
8 import { DataColumns } from "../../components/data-table/data-table";
9
10 interface DataExplorer {
11     columns: DataColumns<any>;
12     items: any[];
13     itemsAvailable: number;
14     page: number;
15     rowsPerPage: number;
16     rowsPerPageOptions?: number[];
17     searchValue: string;
18 }
19
20 export const initialDataExplorer: DataExplorer = {
21     columns: [],
22     items: [],
23     itemsAvailable: 0,
24     page: 0,
25     rowsPerPage: 10,
26     rowsPerPageOptions: [5, 10, 25, 50],
27     searchValue: ""
28 };
29
30 export type DataExplorerState = Record<string, DataExplorer | undefined>;
31
32 const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) =>
33     actions.match(action, {
34         RESET_PAGINATION: ({ id }) =>
35             update(state, id, explorer => ({ ...explorer, page: 0 })),
36
37         SET_COLUMNS: ({ id, columns }) =>
38             update(state, id, setColumns(columns)),
39
40         SET_FILTERS: ({ id, columnName, filters }) =>
41             update(state, id, mapColumns(setFilters(columnName, filters))),
42
43         SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
44             update(state, id, explorer => ({ ...explorer, items, itemsAvailable, page, rowsPerPage })),
45
46         SET_PAGE: ({ id, page }) =>
47             update(state, id, explorer => ({ ...explorer, page })),
48
49         SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) =>
50             update(state, id, explorer => ({ ...explorer, rowsPerPage })),
51
52         TOGGLE_SORT: ({ id, columnName }) =>
53             update(state, id, mapColumns(toggleSort(columnName))),
54
55         TOGGLE_COLUMN: ({ id, columnName }) =>
56             update(state, id, mapColumns(toggleColumn(columnName))),
57
58         default: () => state
59     });
60
61 export default dataExplorerReducer;
62
63 export const getDataExplorer = (state: DataExplorerState, id: string) =>
64     state[id] || initialDataExplorer;
65
66 const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) =>
67     ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
68
69 const setColumns = (columns: DataColumns<any>) =>
70     (dataExplorer: DataExplorer) =>
71         ({ ...dataExplorer, columns });
72
73 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
74     (dataExplorer: DataExplorer) =>
75         ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
76
77 const toggleSort = (columnName: string) =>
78     (column: DataColumn<any>) => column.name === columnName
79         ? toggleSortDirection(column)
80         : resetSortDirection(column);
81
82 const toggleColumn = (columnName: string) =>
83     (column: DataColumn<any>) => column.name === columnName
84         ? { ...column, selected: !column.selected }
85         : column;
86
87 const setFilters = (columnName: string, filters: DataTableFilterItem[]) =>
88     (column: DataColumn<any>) => column.name === columnName
89         ? { ...column, filters }
90         : column;