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, DataTableRequestState } 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[];
25 requestState: DataTableRequestState;
28 export const initialDataExplorer: DataExplorer = {
29 fetchMode: DataTableFetchMode.PAGINATED,
35 rowsPerPageOptions: [50, 100, 200, 500],
37 requestState: DataTableRequestState.IDLE
40 export type DataExplorerState = Record<string, DataExplorer>;
42 export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) =>
43 dataExplorerActions.match(action, {
45 update(state, id, explorer => ({ ...explorer, page: 0, itemsAvailable: 0, items: [] })),
47 RESET_PAGINATION: ({ id }) =>
48 update(state, id, explorer => ({ ...explorer, page: 0 })),
50 SET_FETCH_MODE: ({ id, fetchMode }) =>
51 update(state, id, explorer => ({ ...explorer, fetchMode })),
53 SET_COLUMNS: ({ id, columns }) =>
54 update(state, id, setColumns(columns)),
56 SET_FILTERS: ({ id, columnName, filters }) =>
57 update(state, id, mapColumns(setFilters(columnName, filters))),
59 SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
60 update(state, id, explorer => ({ ...explorer, items, itemsAvailable, page, rowsPerPage })),
62 APPEND_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
63 update(state, id, explorer => ({
65 items: state[id].items.concat(items),
66 itemsAvailable: state[id].itemsAvailable + itemsAvailable,
71 SET_PAGE: ({ id, page }) =>
72 update(state, id, explorer => ({ ...explorer, page })),
74 SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) =>
75 update(state, id, explorer => ({ ...explorer, rowsPerPage })),
77 SET_EXPLORER_SEARCH_VALUE: ({ id, searchValue }) =>
78 update(state, id, explorer => ({ ...explorer, searchValue })),
80 SET_REQUEST_STATE: ({ id, requestState }) =>
81 update(state, id, explorer => ({ ...explorer, requestState })),
83 TOGGLE_SORT: ({ id, columnName }) =>
84 update(state, id, mapColumns(toggleSort(columnName))),
86 TOGGLE_COLUMN: ({ id, columnName }) =>
87 update(state, id, mapColumns(toggleColumn(columnName))),
92 export const getDataExplorer = (state: DataExplorerState, id: string) =>
93 state[id] || initialDataExplorer;
95 export const getSortColumn = (dataExplorer: DataExplorer) => dataExplorer.columns.find((c: any) =>
96 !!c.sortDirection && c.sortDirection !== SortDirection.NONE);
98 const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) =>
99 ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
101 const canUpdateColumns = (prevColumns: DataColumns<any>, nextColumns: DataColumns<any>) => {
102 if (prevColumns.length !== nextColumns.length) {
105 for (let i = 0; i < nextColumns.length; i++) {
106 const pc = prevColumns[i];
107 const nc = nextColumns[i];
108 if (pc.key !== nc.key || pc.name !== nc.name) {
115 const setColumns = (columns: DataColumns<any>) =>
116 (dataExplorer: DataExplorer) =>
117 ({ ...dataExplorer, columns: canUpdateColumns(dataExplorer.columns, columns) ? columns : dataExplorer.columns });
119 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
120 (dataExplorer: DataExplorer) =>
121 ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
123 const toggleSort = (columnName: string) =>
124 (column: DataColumn<any>) => column.name === columnName
125 ? toggleSortDirection(column)
126 : resetSortDirection(column);
128 const toggleColumn = (columnName: string) =>
129 (column: DataColumn<any>) => column.name === columnName
130 ? { ...column, selected: !column.selected }
133 const setFilters = (columnName: string, filters: DataTableFilters) =>
134 (column: DataColumn<any>) => column.name === columnName
135 ? { ...column, filters }