1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
10 export interface DataExplorer {
11 columns: DataColumns<any>;
13 itemsAvailable: number;
16 rowsPerPageOptions?: number[];
20 export const initialDataExplorer: DataExplorer = {
26 rowsPerPageOptions: [5, 10, 25, 50],
30 export type DataExplorerState = Record<string, DataExplorer>;
32 export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) =>
33 dataExplorerActions.match(action, {
34 RESET_PAGINATION: ({ id }) =>
35 update(state, id, explorer => ({ ...explorer, page: 0 })),
37 SET_COLUMNS: ({ id, columns }) =>
38 update(state, id, setColumns(columns)),
40 SET_FILTERS: ({ id, columnName, filters }) =>
41 update(state, id, mapColumns(setFilters(columnName, filters))),
43 SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
44 update(state, id, explorer => ({ ...explorer, items, itemsAvailable, page, rowsPerPage })),
46 SET_PAGE: ({ id, page }) =>
47 update(state, id, explorer => ({ ...explorer, page })),
49 SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) =>
50 update(state, id, explorer => ({ ...explorer, rowsPerPage })),
52 SET_SEARCH_VALUE: ({ id, searchValue }) =>
53 update(state, id, explorer => ({ ...explorer, searchValue })),
55 TOGGLE_SORT: ({ id, columnName }) =>
56 update(state, id, mapColumns(toggleSort(columnName))),
58 TOGGLE_COLUMN: ({ id, columnName }) =>
59 update(state, id, mapColumns(toggleColumn(columnName))),
64 export const getDataExplorer = (state: DataExplorerState, id: string) =>
65 state[id] || initialDataExplorer;
67 const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) =>
68 ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
70 const setColumns = (columns: DataColumns<any>) =>
71 (dataExplorer: DataExplorer) =>
72 ({ ...dataExplorer, columns });
74 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
75 (dataExplorer: DataExplorer) =>
76 ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
78 const toggleSort = (columnName: string) =>
79 (column: DataColumn<any>) => column.name === columnName
80 ? toggleSortDirection(column)
81 : resetSortDirection(column);
83 const toggleColumn = (columnName: string) =>
84 (column: DataColumn<any>) => column.name === columnName
85 ? { ...column, selected: !column.selected }
88 const setFilters = (columnName: string, filters: DataTableFilterItem[]) =>
89 (column: DataColumn<any>) => column.name === columnName
90 ? { ...column, filters }