1fde652d07b3941bfa9b02207352101a72a4ba4a
[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 }
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>;
31
32 export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) =>
33     dataExplorerActions.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         SET_SEARCH_VALUE: ({ id, searchValue }) =>
53             update(state, id, explorer => ({ ...explorer, searchValue })),
54
55         TOGGLE_SORT: ({ id, columnName }) =>
56             update(state, id, mapColumns(toggleSort(columnName))),
57
58         TOGGLE_COLUMN: ({ id, columnName }) =>
59             update(state, id, mapColumns(toggleColumn(columnName))),
60
61         default: () => state
62     });
63
64 export const getDataExplorer = (state: DataExplorerState, id: string) =>
65     state[id] || initialDataExplorer;
66
67 const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) =>
68     ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
69
70 const setColumns = (columns: DataColumns<any>) =>
71     (dataExplorer: DataExplorer) =>
72         ({ ...dataExplorer, columns });
73
74 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
75     (dataExplorer: DataExplorer) =>
76         ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
77
78 const toggleSort = (columnName: string) =>
79     (column: DataColumn<any>) => column.name === columnName
80         ? toggleSortDirection(column)
81         : resetSortDirection(column);
82
83 const toggleColumn = (columnName: string) =>
84     (column: DataColumn<any>) => column.name === columnName
85         ? { ...column, selected: !column.selected }
86         : column;
87
88 const setFilters = (columnName: string, filters: DataTableFilterItem[]) =>
89     (column: DataColumn<any>) => column.name === columnName
90         ? { ...column, filters }
91         : column;