Refactor to apply global navigation actions
[arvados-workbench2.git] / src / store / data-explorer / data-explorer-middleware-service.ts
index 444e74006b8d01cac75e83bec11657850e92b7f0..059c078429833487c553aeb744eaf75ab8a771c7 100644 (file)
@@ -2,21 +2,44 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { getDataExplorer } from "./data-explorer-reducer";
-import { MiddlewareAPI } from "../../../node_modules/redux";
-import { DataColumns } from "../../components/data-table/data-table";
+import { Dispatch, MiddlewareAPI } from "redux";
+import { RootState } from "../store";
+import { DataColumns } from "~/components/data-table/data-table";
+import { DataTableFilterItem } from "~/components/data-table-filters/data-table-filters";
+import { DataExplorer } from './data-explorer-reducer';
+import { ListArguments, ListResults } from '~/common/api/common-resource-service';
 
 export abstract class DataExplorerMiddlewareService {
+    protected readonly id: string;
 
-    abstract get Id(): string;
-    abstract get Columns(): DataColumns<any>;
-    abstract requestItems (api: MiddlewareAPI): void;
-    
-    protected api: MiddlewareAPI;
-    set Api(value: MiddlewareAPI) {
-        this.api = value;
+    protected constructor(id: string) {
+        this.id = id;
     }
-    get DataExplorer () {
-        return getDataExplorer(this.api.getState(), this.Id);
+
+    public getId() {
+        return this.id;
+    }
+
+    public getColumnFilters<T, F extends DataTableFilterItem>(columns: DataColumns<T, F>, columnName: string): F[] {
+        const column = columns.find(c => c.name === columnName);
+        return column ? column.filters.filter(f => f.selected) : [];
     }
-}
\ No newline at end of file
+
+    abstract requestItems(api: MiddlewareAPI<Dispatch, RootState>): void;
+}
+
+export const getDataExplorerColumnFilters = <T, F extends DataTableFilterItem>(columns: DataColumns<T, F>, columnName: string): F[] => {
+    const column = columns.find(c => c.name === columnName);
+    return column ? column.filters.filter(f => f.selected) : [];
+};
+
+export const dataExplorerToListParams = <R>(dataExplorer: DataExplorer) => ({
+    limit: dataExplorer.rowsPerPage,
+    offset: dataExplorer.page * dataExplorer.rowsPerPage,
+});
+
+export const listResultsToDataExplorerItemsMeta = <R>({ itemsAvailable, offset, limit }: ListResults<R>) => ({
+    itemsAvailable,
+    page: Math.floor(offset / limit),
+    rowsPerPage: limit
+});
\ No newline at end of file