Merge branch 'master' into 13857-workflow-view
[arvados-workbench2.git] / src / store / workflow-panel / workflow-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ServiceRepository } from '~/services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService, dataExplorerToListParams, listResultsToDataExplorerItemsMeta } from '~/store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from '~/store/store';
9 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
10 import { DataExplorer, getDataExplorer } from '~/store/data-explorer/data-explorer-reducer';
11 import { updateResources } from '~/store/resources/resources-actions';
12 import { FilterBuilder } from '~/services/api/filter-builder';
13 import { SortDirection } from '~/components/data-table/data-column';
14 import { WorkflowPanelColumnNames } from '~/views/workflow-panel/workflow-panel-view';
15 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
16 import { WorkflowResource } from '~/models/workflow';
17 import { ListResults } from '~/services/common-service/common-resource-service';
18 import { workflowPanelActions } from './workflow-panel-actions';
19
20 export class WorkflowMiddlewareService extends DataExplorerMiddlewareService {
21     constructor(private services: ServiceRepository, id: string) {
22         super(id);
23     }
24
25     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
26         const state = api.getState();
27         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
28         try {
29             const response = await this.services.workflowService.list(getParams(dataExplorer));
30             api.dispatch(updateResources(response.items));
31             api.dispatch(setItems(response));
32         } catch {
33             api.dispatch(couldNotFetchWorkflows());
34         }
35     }
36 }
37
38 export const getParams = (dataExplorer: DataExplorer) => ({
39     ...dataExplorerToListParams(dataExplorer),
40     order: getOrder(dataExplorer),
41     filters: getFilters(dataExplorer)
42 });
43
44 export const getFilters = (dataExplorer: DataExplorer) => {
45     const filters = new FilterBuilder()
46         .addILike("name", dataExplorer.searchValue)
47         .getFilters();
48     return filters;
49 };
50
51 export const getOrder = (dataExplorer: DataExplorer) => {
52     const sortColumn = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
53     const order = new OrderBuilder<WorkflowResource>();
54     if (sortColumn) {
55         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
56             ? OrderDirection.ASC
57             : OrderDirection.DESC;
58         const columnName = sortColumn && sortColumn.name === WorkflowPanelColumnNames.NAME ? "name" : "modifiedAt";
59         return order
60             .addOrder(sortDirection, columnName)
61             .getOrder();
62     } else {
63         return order.getOrder();
64     }
65 };
66
67 export const setItems = (listResults: ListResults<WorkflowResource>) =>
68     workflowPanelActions.SET_ITEMS({
69         ...listResultsToDataExplorerItemsMeta(listResults),
70         items: listResults.items.map(resource => resource.uuid),
71     });
72
73 const couldNotFetchWorkflows = () =>
74     snackbarActions.OPEN_SNACKBAR({
75         message: 'Could not fetch workflows.',
76         kind: SnackbarKind.ERROR
77     });