1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 import { matchRegisteredWorkflowRoute } from 'routes/routes';
17 import { ProcessesMiddlewareService } from "store/processes/processes-middleware-service";
18 import { workflowProcessesPanelActions } from "./workflow-panel-actions";
19 import { joinFilters } from "services/api/filter-builder";
21 export class WorkflowMiddlewareService extends DataExplorerMiddlewareService {
22 constructor(private services: ServiceRepository, id: string) {
26 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
27 const state = api.getState();
28 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
30 const response = await this.services.workflowService.list(getParams(dataExplorer));
31 api.dispatch(updateResources(response.items));
32 api.dispatch(setItems(response));
34 api.dispatch(couldNotFetchWorkflows());
39 export const getParams = (dataExplorer: DataExplorer) => ({
40 ...dataExplorerToListParams(dataExplorer),
41 order: getOrder<WorkflowResource>(dataExplorer),
42 filters: getFilters(dataExplorer)
45 export const getFilters = (dataExplorer: DataExplorer) => {
46 const filters = new FilterBuilder()
47 .addILike("name", dataExplorer.searchValue)
52 export const setItems = (listResults: ListResults<WorkflowResource>) =>
53 workflowPanelActions.SET_ITEMS({
54 ...listResultsToDataExplorerItemsMeta(listResults),
55 items: listResults.items.map(resource => resource.uuid),
58 const couldNotFetchWorkflows = () =>
59 snackbarActions.OPEN_SNACKBAR({
60 message: 'Could not fetch workflows.',
61 kind: SnackbarKind.ERROR
65 export class WorkflowProcessesMiddlewareService extends ProcessesMiddlewareService {
66 constructor(services: ServiceRepository, id: string) {
67 super(services, workflowProcessesPanelActions, id);
70 getFilters(api: MiddlewareAPI<Dispatch, RootState>, dataExplorer: DataExplorer): string | null {
71 const state = api.getState();
73 if (!state.router.location) { return null; }
75 const registeredWorkflowMatch = matchRegisteredWorkflowRoute(state.router.location.pathname);
76 if (!registeredWorkflowMatch) { return null; }
78 const workflow_uuid = registeredWorkflowMatch.params.id;
80 const requesting_container = new FilterBuilder().addEqual('properties.template_uuid', workflow_uuid).getFilters();
81 const sup = super.getFilters(api, dataExplorer);
82 if (sup === null) { return null; }
84 return joinFilters(sup, requesting_container);