1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
10 } from 'components/data-table/data-column';
14 DataTableRequestState,
15 } from './data-explorer-action';
19 } from 'components/data-table/data-table';
20 import { DataTableFilters } from 'components/data-table-filters/data-table-filters-tree';
22 export interface DataExplorer {
23 fetchMode: DataTableFetchMode;
24 columns: DataColumns<any, any>;
26 itemsAvailable: number;
29 rowsPerPageOptions: number[];
32 requestState: DataTableRequestState;
35 export const initialDataExplorer: DataExplorer = {
36 fetchMode: DataTableFetchMode.PAGINATED,
42 rowsPerPageOptions: [10, 20, 50, 100, 200, 500],
44 requestState: DataTableRequestState.IDLE,
47 export type DataExplorerState = Record<string, DataExplorer>;
49 export const dataExplorerReducer = (
50 state: DataExplorerState = {},
51 action: DataExplorerAction
53 return dataExplorerActions.match(action, {
55 update(state, id, (explorer) => ({
62 RESET_PAGINATION: ({ id }) =>
63 update(state, id, (explorer) => ({ ...explorer, page: 0 })),
65 SET_FETCH_MODE: ({ id, fetchMode }) =>
66 update(state, id, (explorer) => ({ ...explorer, fetchMode })),
68 SET_COLUMNS: ({ id, columns }) => update(state, id, setColumns(columns)),
70 SET_FILTERS: ({ id, columnName, filters }) =>
71 update(state, id, mapColumns(setFilters(columnName, filters))),
73 SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
74 update(state, id, (explorer) => ({
82 APPEND_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
83 update(state, id, (explorer) => ({
85 items: state[id].items.concat(items),
86 itemsAvailable: state[id].itemsAvailable + itemsAvailable,
91 SET_PAGE: ({ id, page }) =>
92 update(state, id, (explorer) => ({ ...explorer, page })),
94 SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) =>
95 update(state, id, (explorer) => ({ ...explorer, rowsPerPage })),
97 SET_EXPLORER_SEARCH_VALUE: ({ id, searchValue }) =>
98 update(state, id, (explorer) => ({ ...explorer, searchValue })),
100 RESET_EXPLORER_SEARCH_VALUE: ({ id }) =>
101 update(state, id, (explorer) => ({ ...explorer, searchValue: '' })),
103 SET_REQUEST_STATE: ({ id, requestState }) =>
104 update(state, id, (explorer) => ({ ...explorer, requestState })),
106 TOGGLE_SORT: ({ id, columnName }) =>
107 update(state, id, mapColumns(toggleSort(columnName))),
109 TOGGLE_COLUMN: ({ id, columnName }) =>
110 update(state, id, mapColumns(toggleColumn(columnName))),
112 default: () => state,
115 export const getDataExplorer = (state: DataExplorerState, id: string) => {
116 const returnValue = state[id] || initialDataExplorer;
120 export const getSortColumn = <R>(dataExplorer: DataExplorer): DataColumn<any, R> | undefined =>
121 dataExplorer.columns.find(
122 (c: DataColumn<any, R>) => !!c.sort && c.sort.direction !== SortDirection.NONE
126 state: DataExplorerState,
128 updateFn: (dataExplorer: DataExplorer) => DataExplorer
129 ) => ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
131 const canUpdateColumns = (
132 prevColumns: DataColumns<any, any>,
133 nextColumns: DataColumns<any, any>
135 if (prevColumns.length !== nextColumns.length) {
138 for (let i = 0; i < nextColumns.length; i++) {
139 const pc = prevColumns[i];
140 const nc = nextColumns[i];
141 if (pc.key !== nc.key || pc.name !== nc.name) {
149 (columns: DataColumns<any, any>) => (dataExplorer: DataExplorer) => ({
151 columns: canUpdateColumns(dataExplorer.columns, columns)
153 : dataExplorer.columns,
157 (mapFn: (column: DataColumn<any, any>) => DataColumn<any, any>) =>
158 (dataExplorer: DataExplorer) => ({
160 columns: dataExplorer.columns.map(mapFn),
163 const toggleSort = (columnName: string) => (column: DataColumn<any, any>) =>
164 column.name === columnName
165 ? toggleSortDirection(column)
166 : resetSortDirection(column);
168 const toggleColumn = (columnName: string) => (column: DataColumn<any, any>) =>
169 column.name === columnName
170 ? { ...column, selected: !column.selected }
174 (columnName: string, filters: DataTableFilters) =>
175 (column: DataColumn<any, any>) =>
176 column.name === columnName ? { ...column, filters } : column;