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[];
21 export const initialDataExplorer: DataExplorer = {
27 rowsPerPageOptions: [5, 10, 25, 50],
31 export type DataExplorerState = Record<string, DataExplorer>;
33 export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) =>
34 dataExplorerActions.match(action, {
35 RESET_PAGINATION: ({ id }) =>
36 update(state, id, explorer => ({ ...explorer, page: 0 })),
38 SET_COLUMNS: ({ id, columns }) =>
39 update(state, id, setColumns(columns)),
41 SET_FILTERS: ({ id, columnName, filters }) =>
42 update(state, id, mapColumns(setFilters(columnName, filters))),
44 SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) =>
45 update(state, id, explorer => ({ ...explorer, items, itemsAvailable, page, rowsPerPage })),
47 SET_PAGE: ({ id, page }) =>
48 update(state, id, explorer => ({ ...explorer, page })),
50 SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) =>
51 update(state, id, explorer => ({ ...explorer, rowsPerPage })),
53 SET_SEARCH_VALUE: ({ id, searchValue }) =>
54 update(state, id, explorer => ({ ...explorer, searchValue })),
56 TOGGLE_SORT: ({ id, columnName }) =>
57 update(state, id, mapColumns(toggleSort(columnName))),
59 TOGGLE_COLUMN: ({ id, columnName }) =>
60 update(state, id, mapColumns(toggleColumn(columnName))),
65 export const getDataExplorer = (state: DataExplorerState, id: string) =>
66 state[id] || initialDataExplorer;
68 const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) =>
69 ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
71 const canUpdateColumns = (prevColumns: DataColumns<any>, nextColumns: DataColumns<any>) => {
72 if (prevColumns.length !== nextColumns.length) {
75 for (let i = 0; i < nextColumns.length; i++) {
76 const pc = prevColumns[i];
77 const nc = nextColumns[i];
78 if (pc.key !== nc.key || pc.name !== nc.name) {
85 const setColumns = (columns: DataColumns<any>) =>
86 (dataExplorer: DataExplorer) =>
87 ({ ...dataExplorer, columns: canUpdateColumns(dataExplorer.columns, columns) ? columns : dataExplorer.columns });
89 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
90 (dataExplorer: DataExplorer) =>
91 ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
93 const toggleSort = (columnName: string) =>
94 (column: DataColumn<any>) => column.name === columnName
95 ? toggleSortDirection(column)
96 : resetSortDirection(column);
98 const toggleColumn = (columnName: string) =>
99 (column: DataColumn<any>) => column.name === columnName
100 ? { ...column, selected: !column.selected }
103 const setFilters = (columnName: string, filters: DataTableFilterItem[]) =>
104 (column: DataColumn<any>) => column.name === columnName
105 ? { ...column, filters }