Refactor to apply global navigation actions
[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 { DataTableFilterItem } from "~/components/data-table-filters/data-table-filters";
9 import { DataExplorer } from './data-explorer-reducer';
10 import { ListArguments, ListResults } from '~/common/api/common-resource-service';
11
12 export abstract class DataExplorerMiddlewareService {
13     protected readonly id: string;
14
15     protected constructor(id: string) {
16         this.id = id;
17     }
18
19     public getId() {
20         return this.id;
21     }
22
23     public getColumnFilters<T, F extends DataTableFilterItem>(columns: DataColumns<T, F>, columnName: string): F[] {
24         const column = columns.find(c => c.name === columnName);
25         return column ? column.filters.filter(f => f.selected) : [];
26     }
27
28     abstract requestItems(api: MiddlewareAPI<Dispatch, RootState>): void;
29 }
30
31 export const getDataExplorerColumnFilters = <T, F extends DataTableFilterItem>(columns: DataColumns<T, F>, columnName: string): F[] => {
32     const column = columns.find(c => c.name === columnName);
33     return column ? column.filters.filter(f => f.selected) : [];
34 };
35
36 export const dataExplorerToListParams = <R>(dataExplorer: DataExplorer) => ({
37     limit: dataExplorer.rowsPerPage,
38     offset: dataExplorer.page * dataExplorer.rowsPerPage,
39 });
40
41 export const listResultsToDataExplorerItemsMeta = <R>({ itemsAvailable, offset, limit }: ListResults<R>) => ({
42     itemsAvailable,
43     page: Math.floor(offset / limit),
44     rowsPerPage: limit
45 });