Fix typo
[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 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
20
21 export class WorkflowMiddlewareService extends DataExplorerMiddlewareService {
22     constructor(private services: ServiceRepository, id: string) {
23         super(id);
24     }
25
26     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
27         const state = api.getState();
28         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
29         try {
30             const response = await this.services.workflowService.list(getParams(dataExplorer));
31             api.dispatch(updateResources(response.items));
32             api.dispatch(setItems(response));
33         } catch {
34             api.dispatch(couldNotFetchWorkflows());
35         }
36     }
37 }
38
39 export const getParams = (dataExplorer: DataExplorer) => ({
40     ...dataExplorerToListParams(dataExplorer),
41     order: getOrder(dataExplorer),
42     filters: getFilters(dataExplorer)
43 });
44
45 export const getFilters = (dataExplorer: DataExplorer) => {
46     const filters = new FilterBuilder()
47         .addILike("name", dataExplorer.searchValue)
48         .getFilters();
49     return filters;
50 };
51
52 export const getOrder = (dataExplorer: DataExplorer) => {
53     const sortColumn = getSortColumn(dataExplorer);
54     const order = new OrderBuilder<WorkflowResource>();
55     if (sortColumn) {
56         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
57             ? OrderDirection.ASC
58             : OrderDirection.DESC;
59         const columnName = sortColumn && sortColumn.name === WorkflowPanelColumnNames.NAME ? "name" : "modifiedAt";
60         return order
61             .addOrder(sortDirection, columnName)
62             .getOrder();
63     } else {
64         return order.getOrder();
65     }
66 };
67
68 export const setItems = (listResults: ListResults<WorkflowResource>) =>
69     workflowPanelActions.SET_ITEMS({
70         ...listResultsToDataExplorerItemsMeta(listResults),
71         items: listResults.items.map(resource => resource.uuid),
72     });
73
74 const couldNotFetchWorkflows = () =>
75     snackbarActions.OPEN_SNACKBAR({
76         message: 'Could not fetch workflows.',
77         kind: SnackbarKind.ERROR
78     });