19231: Add smaller page sizes (10 and 20 items) to load faster
[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 {
6     DataColumn,
7     resetSortDirection,
8     SortDirection,
9     toggleSortDirection
10 } from "components/data-table/data-column";
11 import { DataExplorerAction, dataExplorerActions, DataTableRequestState } from "./data-explorer-action";
12 import { DataColumns, DataTableFetchMode } from "components/data-table/data-table";
13 import { DataTableFilters } from "components/data-table-filters/data-table-filters-tree";
14
15 export interface DataExplorer {
16     fetchMode: DataTableFetchMode;
17     columns: DataColumns<any>;
18     items: any[];
19     itemsAvailable: number;
20     page: number;
21     rowsPerPage: number;
22     rowsPerPageOptions: number[];
23     searchValue: string;
24     working?: boolean;
25     requestState: DataTableRequestState;
26 }
27
28 export const initialDataExplorer: DataExplorer = {
29     fetchMode: DataTableFetchMode.PAGINATED,
30     columns: [],
31     items: [],
32     itemsAvailable: 0,
33     page: 0,
34     rowsPerPage: 50,
35     rowsPerPageOptions: [10, 20, 50, 100, 200, 500],
36     searchValue: "",
37     requestState: DataTableRequestState.IDLE
38 };
39
40 export type DataExplorerState = Record<string, DataExplorer>;
41
42 export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) =>
43     dataExplorerActions.match(action, {
44         CLEAR: ({ id }) =>
45             update(state, id, explorer => ({ ...explorer, page: 0, itemsAvailable: 0, items: [] })),
46
47         RESET_PAGINATION: ({ id }) =>
48             update(state, id, explorer => ({ ...explorer, page: 0 })),
49
50         SET_FETCH_MODE: ({ id, fetchMode }) =>
51             update(state, id, explorer => ({ ...explorer, fetchMode })),
52
53         SET_COLUMNS: ({ id, columns }) =>
54             update(state, id, setColumns(columns)),
55
56         SET_FILTERS: ({ id, columnName, filters }) =>
57             update(state, id, mapColumns(setFilters(columnName, filters))),
58
59         SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
60             update(state, id, explorer => ({ ...explorer, items, itemsAvailable, page: page || 0, rowsPerPage })),
61
62         APPEND_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
63             update(state, id, explorer => ({
64                 ...explorer,
65                 items: state[id].items.concat(items),
66                 itemsAvailable: state[id].itemsAvailable + itemsAvailable,
67                 page,
68                 rowsPerPage
69             })),
70
71         SET_PAGE: ({ id, page }) =>
72             update(state, id, explorer => ({ ...explorer, page })),
73
74         SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) =>
75             update(state, id, explorer => ({ ...explorer, rowsPerPage })),
76
77         SET_EXPLORER_SEARCH_VALUE: ({ id, searchValue }) =>
78             update(state, id, explorer => ({ ...explorer, searchValue })),
79
80         SET_REQUEST_STATE: ({ id, requestState }) =>
81             update(state, id, explorer => ({ ...explorer, requestState })),
82
83         TOGGLE_SORT: ({ id, columnName }) =>
84             update(state, id, mapColumns(toggleSort(columnName))),
85
86         TOGGLE_COLUMN: ({ id, columnName }) =>
87             update(state, id, mapColumns(toggleColumn(columnName))),
88
89         default: () => state
90     });
91
92 export const getDataExplorer = (state: DataExplorerState, id: string) =>
93     state[id] || initialDataExplorer;
94
95 export const getSortColumn = (dataExplorer: DataExplorer) => dataExplorer.columns.find((c: any) =>
96     !!c.sortDirection && c.sortDirection !== SortDirection.NONE);
97
98 const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) =>
99     ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
100
101 const canUpdateColumns = (prevColumns: DataColumns<any>, nextColumns: DataColumns<any>) => {
102     if (prevColumns.length !== nextColumns.length) {
103         return true;
104     }
105     for (let i = 0; i < nextColumns.length; i++) {
106         const pc = prevColumns[i];
107         const nc = nextColumns[i];
108         if (pc.key !== nc.key || pc.name !== nc.name) {
109             return true;
110         }
111     }
112     return false;
113 };
114
115 const setColumns = (columns: DataColumns<any>) =>
116     (dataExplorer: DataExplorer) =>
117         ({ ...dataExplorer, columns: canUpdateColumns(dataExplorer.columns, columns) ? columns : dataExplorer.columns });
118
119 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
120     (dataExplorer: DataExplorer) =>
121         ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
122
123 const toggleSort = (columnName: string) =>
124     (column: DataColumn<any>) => column.name === columnName
125         ? toggleSortDirection(column)
126         : resetSortDirection(column);
127
128 const toggleColumn = (columnName: string) =>
129     (column: DataColumn<any>) => column.name === columnName
130         ? { ...column, selected: !column.selected }
131         : column;
132
133 const setFilters = (columnName: string, filters: DataTableFilters) =>
134     (column: DataColumn<any>) => column.name === columnName
135         ? { ...column, filters }
136         : column;