1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { DataColumn, toggleSortDirection, resetSortDirection, SortDirection } from "~/components/data-table/data-column";
6 import { dataExplorerActions, DataExplorerAction } from "./data-explorer-action";
7 import { DataColumns } from "~/components/data-table/data-table";
8 import { DataTableFilters } from "~/components/data-table-filters/data-table-filters-tree";
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_EXPLORER_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 export const getSortColumn = (dataExplorer: DataExplorer) => dataExplorer.columns.find((c: any) =>
69 !!c.sortDirection && c.sortDirection !== SortDirection.NONE);
71 const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) =>
72 ({ ...state, [id]: updateFn(getDataExplorer(state, id)) });
74 const canUpdateColumns = (prevColumns: DataColumns<any>, nextColumns: DataColumns<any>) => {
75 if (prevColumns.length !== nextColumns.length) {
78 for (let i = 0; i < nextColumns.length; i++) {
79 const pc = prevColumns[i];
80 const nc = nextColumns[i];
81 if (pc.key !== nc.key || pc.name !== nc.name) {
88 const setColumns = (columns: DataColumns<any>) =>
89 (dataExplorer: DataExplorer) =>
90 ({ ...dataExplorer, columns: canUpdateColumns(dataExplorer.columns, columns) ? columns : dataExplorer.columns });
92 const mapColumns = (mapFn: (column: DataColumn<any>) => DataColumn<any>) =>
93 (dataExplorer: DataExplorer) =>
94 ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) });
96 const toggleSort = (columnName: string) =>
97 (column: DataColumn<any>) => column.name === columnName
98 ? toggleSortDirection(column)
99 : resetSortDirection(column);
101 const toggleColumn = (columnName: string) =>
102 (column: DataColumn<any>) => column.name === columnName
103 ? { ...column, selected: !column.selected }
106 const setFilters = (columnName: string, filters: DataTableFilters) =>
107 (column: DataColumn<any>) => column.name === columnName
108 ? { ...column, filters }