19690: tidy up
[arvados-workbench2.git] / src / store / data-explorer / data-explorer-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch, MiddlewareAPI } from 'redux';
6 import { RootState } from '../store';
7 import { DataColumns } from 'components/data-table/data-table';
8 import { DataExplorer } from './data-explorer-reducer';
9 import { ListResults } from 'services/common-service/common-service';
10 import { createTree } from 'models/tree';
11 import { DataTableFilters } from 'components/data-table-filters/data-table-filters-tree';
12
13 export abstract class DataExplorerMiddlewareService {
14   protected readonly id: string;
15
16   protected constructor(id: string) {
17     this.id = id;
18   }
19
20   public getId() {
21     return this.id;
22   }
23
24   public getColumnFilters<T>(
25     columns: DataColumns<T>,
26     columnName: string
27   ): DataTableFilters {
28     return getDataExplorerColumnFilters(columns, columnName);
29   }
30
31   abstract requestItems(
32     api: MiddlewareAPI<Dispatch, RootState>,
33     criteriaChanged?: boolean
34   ): Promise<void>;
35 }
36
37 export const getDataExplorerColumnFilters = <T>(
38   columns: DataColumns<T>,
39   columnName: string
40 ): DataTableFilters => {
41   const column = columns.find((c) => c.name === columnName);
42   return column ? column.filters : createTree();
43 };
44
45 export const dataExplorerToListParams = (dataExplorer: DataExplorer) => ({
46   limit: dataExplorer.rowsPerPage,
47   offset: dataExplorer.page * dataExplorer.rowsPerPage,
48 });
49
50 export const listResultsToDataExplorerItemsMeta = <R>({
51   itemsAvailable,
52   offset,
53   limit,
54 }: ListResults<R>) => ({
55   itemsAvailable,
56   page: Math.floor(offset / limit),
57   rowsPerPage: limit,
58 });