X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/055c1f7ec3450e6e36abfc34d7515e57d9481bdc..24ddc06cb04b124d337e91c190230bfad83e490b:/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 1f782778..c112454b 100644 --- a/src/store/data-explorer/data-explorer-reducer.ts +++ b/src/store/data-explorer/data-explorer-reducer.ts @@ -3,45 +3,71 @@ // SPDX-License-Identifier: AGPL-3.0 import { DataColumn, toggleSortDirection, resetSortDirection } from "../../components/data-table/data-column"; -import actions, { DataExplorerAction } from "./data-explorer-action"; +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"; interface DataExplorer { - columns: Array>; + columns: DataColumns; items: any[]; + itemsAvailable: number; page: number; rowsPerPage: number; + rowsPerPageOptions?: number[]; + searchValue: string; } export const initialDataExplorer: DataExplorer = { columns: [], items: [], + itemsAvailable: 0, page: 0, - rowsPerPage: 0 + rowsPerPage: 10, + rowsPerPageOptions: [5, 10, 25, 50], + searchValue: "" }; export type DataExplorerState = Record; -const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) => - actions.match(action, { - 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 }) => update(state, id, explorer => ({ ...explorer, items })), - SET_PAGE: ({ id, page }) => update(state, id, explorer => ({ ...explorer, page })), - SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) => update(state, id, explorer => ({ ...explorer, rowsPerPage })), - TOGGLE_SORT: ({ id, columnName }) => update(state, id, mapColumns(toggleSort(columnName))), - TOGGLE_COLUMN: ({ id, columnName }) => update(state, id, mapColumns(toggleColumn(columnName))), +export const dataExplorerReducer = (state: DataExplorerState = {}, action: DataExplorerAction) => + dataExplorerActions.match(action, { + RESET_PAGINATION: ({ id }) => + update(state, id, explorer => ({ ...explorer, page: 0 })), + + 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_PAGE: ({ id, page }) => + update(state, id, explorer => ({ ...explorer, page })), + + SET_ROWS_PER_PAGE: ({ id, rowsPerPage }) => + update(state, id, explorer => ({ ...explorer, rowsPerPage })), + + SET_SEARCH_VALUE: ({ id, searchValue }) => + update(state, id, explorer => ({ ...explorer, searchValue })), + + TOGGLE_SORT: ({ id, columnName }) => + update(state, id, mapColumns(toggleSort(columnName))), + + TOGGLE_COLUMN: ({ id, columnName }) => + update(state, id, mapColumns(toggleColumn(columnName))), + default: () => state }); -export default dataExplorerReducer; - -const get = (state: DataExplorerState, id: string) => state[id] || initialDataExplorer; +export const getDataExplorer = (state: DataExplorerState, id: string) => + state[id] || initialDataExplorer; const update = (state: DataExplorerState, id: string, updateFn: (dataExplorer: DataExplorer) => DataExplorer) => - ({ ...state, [id]: updateFn(get(state, id)) }); + ({ ...state, [id]: updateFn(getDataExplorer(state, id)) }); -const setColumns = (columns: Array>) => +const setColumns = (columns: DataColumns) => (dataExplorer: DataExplorer) => ({ ...dataExplorer, columns });