X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/b62944772ff96019c1e497426784690978bb9c96..207a57dad1fad3048964da01aab0baa4d0c81f93:/src/store/data-explorer/data-explorer-reducer.ts diff --git a/src/store/data-explorer/data-explorer-reducer.ts b/src/store/data-explorer/data-explorer-reducer.ts index 02ebece2..a0a7eb64 100644 --- a/src/store/data-explorer/data-explorer-reducer.ts +++ b/src/store/data-explorer/data-explorer-reducer.ts @@ -2,55 +2,116 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { DataColumn, toggleSortDirection, resetSortDirection } from "../../components/data-table/data-column"; -import { dataExplorerActions, DataExplorerAction } from "./data-explorer-action"; -import { DataTableFilterItem } from "../../components/data-table-filters/data-table-filters"; -import { DataColumns } from "../../components/data-table/data-table"; +import { + DataColumn, + resetSortDirection, + SortDirection, + toggleSortDirection, +} from 'components/data-table/data-column'; +import { + DataExplorerAction, + dataExplorerActions, + DataTableRequestState, +} from './data-explorer-action'; +import { + DataColumns, + DataTableFetchMode, +} from 'components/data-table/data-table'; +import { DataTableFilters } from 'components/data-table-filters/data-table-filters-tree'; export interface DataExplorer { - columns: DataColumns; + fetchMode: DataTableFetchMode; + columns: DataColumns; items: any[]; itemsAvailable: number; page: number; rowsPerPage: number; - rowsPerPageOptions?: number[]; + rowsPerPageOptions: number[]; searchValue: string; + working?: boolean; + requestState: DataTableRequestState; } export const initialDataExplorer: DataExplorer = { + fetchMode: DataTableFetchMode.PAGINATED, columns: [], items: [], itemsAvailable: 0, page: 0, - rowsPerPage: 10, - rowsPerPageOptions: [5, 10, 25, 50], - searchValue: "" + rowsPerPage: 50, + rowsPerPageOptions: [10, 20, 50, 100, 200, 500], + searchValue: '', + requestState: DataTableRequestState.IDLE, }; -export type DataExplorerState = Record; +export type DataExplorerState = Record; + +export const dataExplorerReducer = ( + state: DataExplorerState = {}, + action: DataExplorerAction +) => { + return dataExplorerActions.match(action, { + CLEAR: ({ id }) => + update(state, id, (explorer) => ({ + ...explorer, + page: 0, + itemsAvailable: 0, + items: [], + })), -export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) => - dataExplorerActions.match(action, { RESET_PAGINATION: ({ id }) => - update(state, id, explorer => ({ ...explorer, page: 0 })), + update(state, id, (explorer) => ({ ...explorer, page: 0 })), - SET_COLUMNS: ({ id, columns }) => - update(state, id, setColumns(columns)), + SET_FETCH_MODE: ({ id, fetchMode }) => + update(state, id, (explorer) => ({ ...explorer, fetchMode })), + + SET_COLUMNS: ({ id, columns }) => update(state, id, setColumns(columns)), SET_FILTERS: ({ id, columnName, filters }) => update(state, id, mapColumns(setFilters(columnName, filters))), - SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) => - update(state, id, explorer => ({ ...explorer, items, itemsAvailable, page, rowsPerPage })), + SET_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) => ( + update(state, id, (explorer) => { + // Reject updates to pages other than current, + // DataExplorer middleware should retry + const updatedPage = page || 0; + if (explorer.page === updatedPage) { + return { + ...explorer, + items, + itemsAvailable, + page: updatedPage, + rowsPerPage, + } + } else { + return explorer; + } + }) + ), + + APPEND_ITEMS: ({ id, items, itemsAvailable, page, rowsPerPage }) => + update(state, id, (explorer) => ({ + ...explorer, + items: state[id].items.concat(items), + itemsAvailable: state[id].itemsAvailable + itemsAvailable, + page, + rowsPerPage, + })), SET_PAGE: ({ id, page }) => - update(state, id, explorer => ({ ...explorer, page })), + update(state, id, (explorer) => ({ ...explorer, page })), SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) => - update(state, id, explorer => ({ ...explorer, rowsPerPage })), + update(state, id, (explorer) => ({ ...explorer, rowsPerPage })), + + SET_EXPLORER_SEARCH_VALUE: ({ id, searchValue }) => + update(state, id, (explorer) => ({ ...explorer, searchValue })), + + RESET_EXPLORER_SEARCH_VALUE: ({ id }) => + update(state, id, (explorer) => ({ ...explorer, searchValue: '' })), - SET_SEARCH_VALUE: ({ id, searchValue }) => - update(state, id, explorer => ({ ...explorer, searchValue })), + SET_REQUEST_STATE: ({ id, requestState }) => + update(state, id, (explorer) => ({ ...explorer, requestState })), TOGGLE_SORT: ({ id, columnName }) => update(state, id, mapColumns(toggleSort(columnName))), @@ -58,34 +119,68 @@ export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataE TOGGLE_COLUMN: ({ id, columnName }) => update(state, id, mapColumns(toggleColumn(columnName))), - default: () => state + default: () => state, }); +}; +export const getDataExplorer = (state: DataExplorerState, id: string) => { + const returnValue = state[id] || initialDataExplorer; + return returnValue; +}; -export const getDataExplorer = (state: DataExplorerState, id: string) => - state[id] || initialDataExplorer; - -const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) => - ({ ...state, [id]: updateFn(getDataExplorer(state, id)) }); +export const getSortColumn = (dataExplorer: DataExplorer): DataColumn | undefined => + dataExplorer.columns.find( + (c: DataColumn) => !!c.sort && c.sort.direction !== SortDirection.NONE + ); + +const update = ( + state: DataExplorerState, + id: string, + updateFn: (dataExplorer: DataExplorer) => DataExplorer +) => ({ ...state, [id]: updateFn(getDataExplorer(state, id)) }); + +const canUpdateColumns = ( + prevColumns: DataColumns, + nextColumns: DataColumns +) => { + if (prevColumns.length !== nextColumns.length) { + return true; + } + for (let i = 0; i < nextColumns.length; i++) { + const pc = prevColumns[i]; + const nc = nextColumns[i]; + if (pc.key !== nc.key || pc.name !== nc.name) { + return true; + } + } + return false; +}; -const setColumns = (columns: DataColumns) => - (dataExplorer: DataExplorer) => - ({ ...dataExplorer, columns }); +const setColumns = + (columns: DataColumns) => (dataExplorer: DataExplorer) => ({ + ...dataExplorer, + columns: canUpdateColumns(dataExplorer.columns, columns) + ? columns + : dataExplorer.columns, + }); -const mapColumns = (mapFn: (column: DataColumn) => DataColumn) => - (dataExplorer: DataExplorer) => - ({ ...dataExplorer, columns: dataExplorer.columns.map(mapFn) }); +const mapColumns = + (mapFn: (column: DataColumn) => DataColumn) => + (dataExplorer: DataExplorer) => ({ + ...dataExplorer, + columns: dataExplorer.columns.map(mapFn), + }); -const toggleSort = (columnName: string) => - (column: DataColumn) => column.name === columnName +const toggleSort = (columnName: string) => (column: DataColumn) => + column.name === columnName ? toggleSortDirection(column) : resetSortDirection(column); -const toggleColumn = (columnName: string) => - (column: DataColumn) => column.name === columnName +const toggleColumn = (columnName: string) => (column: DataColumn) => + column.name === columnName ? { ...column, selected: !column.selected } : column; -const setFilters = (columnName: string, filters: DataTableFilterItem[]) => - (column: DataColumn) => column.name === columnName - ? { ...column, filters } - : column; +const setFilters = + (columnName: string, filters: DataTableFilters) => + (column: DataColumn) => + column.name === columnName ? { ...column, filters } : column;