1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
10 } from "~/components/data-table/data-column";
11 import { DataExplorerAction, dataExplorerActions } 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";
15 export interface DataExplorer {
16 fetchMode: DataTableFetchMode;
17 columns: DataColumns<any>;
19 itemsAvailable: number;
22 rowsPerPageOptions: number[];
27 export const initialDataExplorer: DataExplorer = {
28 fetchMode: DataTableFetchMode.PAGINATED,
34 rowsPerPageOptions: [5, 10, 25, 50],
38 export type DataExplorerState = Record<string, DataExplorer>;
40 export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) =>
41 dataExplorerActions.match(action, {
43 update(state, id, explorer => ({ ...explorer, page: 0, itemsAvailable: 0, items: [] })),
45 RESET_PAGINATION: ({ id }) =>
46 update(state, id, explorer => ({ ...explorer, page: 0 })),
48 SET_FETCH_MODE: ({ id, fetchMode }) =>
49 update(state, id, explorer => ({ ...explorer, fetchMode })),
51 SET_COLUMNS: ({ id, columns }) =>
52 update(state, id, setColumns(columns)),
54 SET_FILTERS: ({ id, columnName, filters }) =>
55 update(state, id, mapColumns(setFilters(columnName, filters))),
57 SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
58 update(state, id, explorer => ({ ...explorer, items, itemsAvailable, page, rowsPerPage })),
60 APPEND_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
61 update(state, id, explorer => ({
63 items: state[id].items.concat(items),
64 itemsAvailable: state[id].itemsAvailable + itemsAvailable,
69 SET_PAGE: ({ id, page }) =>
70 update(state, id, explorer => ({ ...explorer, page })),
72 SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) =>
73 update(state, id, explorer => ({ ...explorer, rowsPerPage })),
75 SET_EXPLORER_SEARCH_VALUE: ({ id, searchValue }) =>
76 update(state, id, explorer => ({ ...explorer, searchValue })),
78 TOGGLE_SORT: ({ id, columnName }) =>
79 update(state, id, mapColumns(toggleSort(columnName))),
81 TOGGLE_COLUMN: ({ id, columnName }) =>
82 update(state, id, mapColumns(toggleColumn(columnName))),
87 export const getDataExplorer = (state: DataExplorerState, id: string) =>
88 state[id] || initialDataExplorer;
90 export const getSortColumn = (dataExplorer: DataExplorer) => dataExplorer.columns.find((c: any) =>
91 !!c.sortDirection && c.sortDirection !== SortDirection.NONE);
93 const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) =>
94 ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
96 const canUpdateColumns = (prevColumns: DataColumns<any>, nextColumns: DataColumns<any>) => {
97 if (prevColumns.length !== nextColumns.length) {
100 for (let i = 0; i < nextColumns.length; i++) {
101 const pc = prevColumns[i];
102 const nc = nextColumns[i];
103 if (pc.key !== nc.key || pc.name !== nc.name) {
110 const setColumns = (columns: DataColumns<any>) =>
111 (dataExplorer: DataExplorer) =>
112 ({ ...dataExplorer, columns: canUpdateColumns(dataExplorer.columns, columns) ? columns : dataExplorer.columns });
114 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
115 (dataExplorer: DataExplorer) =>
116 ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
118 const toggleSort = (columnName: string) =>
119 (column: DataColumn<any>) => column.name === columnName
120 ? toggleSortDirection(column)
121 : resetSortDirection(column);
123 const toggleColumn = (columnName: string) =>
124 (column: DataColumn<any>) => column.name === columnName
125 ? { ...column, selected: !column.selected }
128 const setFilters = (columnName: string, filters: DataTableFilters) =>
129 (column: DataColumn<any>) => column.name === columnName
130 ? { ...column, filters }