Change default table view to be displayed only after data fetch
[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 { dataExplorerActions, 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 export 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     working?: boolean;
19 }
20
21 export const initialDataExplorer: DataExplorer = {
22     columns: [],
23     items: [],
24     itemsAvailable: 0,
25     page: 0,
26     rowsPerPage: 10,
27     rowsPerPageOptions: [5, 10, 25, 50],
28     searchValue: ""
29 };
30
31 export type DataExplorerState = Record<string, DataExplorer>;
32
33 export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) =>
34     dataExplorerActions.match(action, {
35         RESET_PAGINATION: ({ id }) =>
36             update(state, id, explorer => ({ ...explorer, page: 0 })),
37
38         SET_COLUMNS: ({ id, columns }) =>
39             update(state, id, setColumns(columns)),
40
41         SET_FILTERS: ({ id, columnName, filters }) =>
42             update(state, id, mapColumns(setFilters(columnName, filters))),
43
44         SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
45             update(state, id, explorer => ({ ...explorer, items, itemsAvailable, page, rowsPerPage })),
46
47         SET_PAGE: ({ id, page }) =>
48             update(state, id, explorer => ({ ...explorer, page })),
49
50         SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) =>
51             update(state, id, explorer => ({ ...explorer, rowsPerPage })),
52
53         SET_SEARCH_VALUE: ({ id, searchValue }) =>
54             update(state, id, explorer => ({ ...explorer, searchValue })),
55
56         TOGGLE_SORT: ({ id, columnName }) =>
57             update(state, id, mapColumns(toggleSort(columnName))),
58
59         TOGGLE_COLUMN: ({ id, columnName }) =>
60             update(state, id, mapColumns(toggleColumn(columnName))),
61
62         default: () => state
63     });
64
65 export const getDataExplorer = (state: DataExplorerState, id: string) =>
66     state[id] || initialDataExplorer;
67
68 const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) =>
69     ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
70
71 const canUpdateColumns = (prevColumns: DataColumns<any>, nextColumns: DataColumns<any>) => {
72     if (prevColumns.length !== nextColumns.length) {
73         return true;
74     }
75     for (let i = 0; i < nextColumns.length; i++) {
76         const pc = prevColumns[i];
77         const nc = nextColumns[i];
78         if (pc.key !== nc.key || pc.name !== nc.name) {
79             return true;
80         }
81     }
82     return false;
83 };
84
85 const setColumns = (columns: DataColumns<any>) =>
86     (dataExplorer: DataExplorer) =>
87         ({ ...dataExplorer, columns: canUpdateColumns(dataExplorer.columns, columns) ? columns : dataExplorer.columns });
88
89 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
90     (dataExplorer: DataExplorer) =>
91         ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
92
93 const toggleSort = (columnName: string) =>
94     (column: DataColumn<any>) => column.name === columnName
95         ? toggleSortDirection(column)
96         : resetSortDirection(column);
97
98 const toggleColumn = (columnName: string) =>
99     (column: DataColumn<any>) => column.name === columnName
100         ? { ...column, selected: !column.selected }
101         : column;
102
103 const setFilters = (columnName: string, filters: DataTableFilterItem[]) =>
104     (column: DataColumn<any>) => column.name === columnName
105         ? { ...column, filters }
106         : column;