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 canUpdateColumns = (prevColumns: DataColumns<any>, nextColumns: DataColumns<any>) => {
71 if (prevColumns.length !== nextColumns.length) {
74 for (let i = 0; i < nextColumns.length; i++) {
75 const pc = prevColumns[i];
76 const nc = nextColumns[i];
77 if (pc.key !== nc.key || pc.name !== nc.name) {
84 const setColumns = (columns: DataColumns<any>) =>
85 (dataExplorer: DataExplorer) =>
86 ({ ...dataExplorer, columns: canUpdateColumns(dataExplorer.columns, columns) ? columns : dataExplorer.columns });
88 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
89 (dataExplorer: DataExplorer) =>
90 ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
92 const toggleSort = (columnName: string) =>
93 (column: DataColumn<any>) => column.name === columnName
94 ? toggleSortDirection(column)
95 : resetSortDirection(column);
97 const toggleColumn = (columnName: string) =>
98 (column: DataColumn<any>) => column.name === columnName
99 ? { ...column, selected: !column.selected }
102 const setFilters = (columnName: string, filters: DataTableFilterItem[]) =>
103 (column: DataColumn<any>) => column.name === columnName
104 ? { ...column, filters }