20031: Add type for collection file operation location for form field
[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, getOrder, 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 { WorkflowResource } from 'models/workflow';
14 import { ListResults } from 'services/common-service/common-service';
15 import { workflowPanelActions } from 'store/workflow-panel/workflow-panel-actions';
16
17 export class WorkflowMiddlewareService extends DataExplorerMiddlewareService {
18     constructor(private services: ServiceRepository, id: string) {
19         super(id);
20     }
21
22     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
23         const state = api.getState();
24         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
25         try {
26             const response = await this.services.workflowService.list(getParams(dataExplorer));
27             api.dispatch(updateResources(response.items));
28             api.dispatch(setItems(response));
29         } catch {
30             api.dispatch(couldNotFetchWorkflows());
31         }
32     }
33 }
34
35 export const getParams = (dataExplorer: DataExplorer) => ({
36     ...dataExplorerToListParams(dataExplorer),
37     order: getOrder<WorkflowResource>(dataExplorer),
38     filters: getFilters(dataExplorer)
39 });
40
41 export const getFilters = (dataExplorer: DataExplorer) => {
42     const filters = new FilterBuilder()
43         .addILike("name", dataExplorer.searchValue)
44         .getFilters();
45     return filters;
46 };
47
48 export const setItems = (listResults: ListResults<WorkflowResource>) =>
49     workflowPanelActions.SET_ITEMS({
50         ...listResultsToDataExplorerItemsMeta(listResults),
51         items: listResults.items.map(resource => resource.uuid),
52     });
53
54 const couldNotFetchWorkflows = () =>
55     snackbarActions.OPEN_SNACKBAR({
56         message: 'Could not fetch workflows.',
57         kind: SnackbarKind.ERROR
58     });