1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch, MiddlewareAPI } from 'redux';
6 import { RootState } from '../store';
7 import { DataExplorer, getSortColumn } from './data-explorer-reducer';
8 import { ListResults } from 'services/common-service/common-service';
9 import { createTree } from 'models/tree';
10 import { DataTableFilters } from 'components/data-table-filters/data-table-filters';
11 import { OrderBuilder, OrderDirection } from 'services/api/order-builder';
12 import { DataColumns, SortDirection } from 'components/data-table/data-column';
13 import { Resource } from 'models/resource';
15 export abstract class DataExplorerMiddlewareService {
16 protected readonly id: string;
18 protected constructor(id: string) {
26 public getColumnFilters<T>(
27 columns: DataColumns<T, any>,
30 return getDataExplorerColumnFilters(columns, columnName);
34 * Consumers can use this method to request
35 * total count separately and in parallel
37 * @param criteriaChanged
40 abstract requestCount(
41 api: MiddlewareAPI<Dispatch, RootState>,
42 criteriaChanged?: boolean,
46 abstract requestItems(
47 api: MiddlewareAPI<Dispatch, RootState>,
48 criteriaChanged?: boolean,
53 export const getDataExplorerColumnFilters = <T>(
54 columns: DataColumns<T, any>,
56 ): DataTableFilters => {
57 const column = columns.find((c) => c.name === columnName);
58 return column ? column.filters : createTree();
61 export const dataExplorerToListParams = (dataExplorer: DataExplorer) => ({
62 limit: dataExplorer.rowsPerPage,
63 offset: dataExplorer.page * dataExplorer.rowsPerPage,
66 export const getOrder = <T extends Resource = Resource>(dataExplorer: DataExplorer) => {
67 const sortColumn = getSortColumn<T>(dataExplorer);
68 const order = new OrderBuilder<T>();
69 if (sortColumn && sortColumn.sort) {
70 const sortDirection = sortColumn.sort.direction === SortDirection.ASC
72 : OrderDirection.DESC;
74 // Use createdAt as a secondary sort column so we break ties consistently.
76 .addOrder(sortDirection, sortColumn.sort.field)
77 .addOrder(OrderDirection.DESC, "createdAt")
80 return order.getOrder();
84 export type DataExplorerMeta = {
85 itemsAvailable?: number;
90 export const listResultsToDataExplorerItemsMeta = <R>({
94 }: ListResults<R>): DataExplorerMeta => ({
96 page: Math.floor(offset / limit),