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, 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-service';
18 import { workflowPanelActions } from 'store/workflow-panel/workflow-panel-actions';
19 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
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(dataExplorer),
42 filters: getFilters(dataExplorer)
45 export const getFilters = (dataExplorer: DataExplorer) => {
46 const filters = new FilterBuilder()
47 .addILike("name", dataExplorer.searchValue)
52 export const getOrder = (dataExplorer: DataExplorer) => {
53 const sortColumn = getSortColumn(dataExplorer);
54 const order = new OrderBuilder<WorkflowResource>();
56 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
58 : OrderDirection.DESC;
59 const columnName = sortColumn && sortColumn.name === WorkflowPanelColumnNames.NAME ? "name" : "modifiedAt";
61 .addOrder(sortDirection, columnName)
64 return order.getOrder();
68 export const setItems = (listResults: ListResults<WorkflowResource>) =>
69 workflowPanelActions.SET_ITEMS({
70 ...listResultsToDataExplorerItemsMeta(listResults),
71 items: listResults.items.map(resource => resource.uuid),
74 const couldNotFetchWorkflows = () =>
75 snackbarActions.OPEN_SNACKBAR({
76 message: 'Could not fetch workflows.',
77 kind: SnackbarKind.ERROR