api-response
[arvados-workbench2.git] / src / store / search-results-panel / search-results-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 { SortDirection } from '~/components/data-table/data-column';
13 import { SearchResultsPanelColumnNames } from '~/views/search-results-panel/search-results-panel-view';
14 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
15 import { GroupContentsResource, GroupContentsResourcePrefix } from "~/services/groups-service/groups-service";
16 import { ListResults } from '~/services/common-service/common-resource-service';
17 import { searchResultsPanelActions } from '~/store/search-results-panel/search-results-panel-actions';
18 import { getFilters } from '~/store/search-bar/search-bar-actions';
19
20 export class SearchResultsMiddlewareService 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 userUuid = state.auth.user!.uuid;
28         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
29         const searchValue = state.searchBar.searchValue;
30         try {
31             const response = await this.services.groupsService.contents(userUuid, getParams(dataExplorer, searchValue));
32             api.dispatch(updateResources(response.items));
33             api.dispatch(setItems(response));
34         } catch {
35             api.dispatch(couldNotFetchWorkflows());
36         }
37     }
38 }
39
40 export const getParams = (dataExplorer: DataExplorer, searchValue: string) => ({
41     ...dataExplorerToListParams(dataExplorer),
42     filters: getFilters('name', searchValue),
43     order: getOrder(dataExplorer)
44 });
45
46 const getOrder = (dataExplorer: DataExplorer) => {
47     const sortColumn = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
48     const order = new OrderBuilder<GroupContentsResource>();
49     if (sortColumn) {
50         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
51             ? OrderDirection.ASC
52             : OrderDirection.DESC;
53
54         const columnName = sortColumn && sortColumn.name === SearchResultsPanelColumnNames.NAME ? "name" : "modifiedAt";
55         return order
56             .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
57             .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
58             .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
59             .getOrder();
60     } else {
61         return order.getOrder();
62     }
63 };
64
65 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
66     searchResultsPanelActions.SET_ITEMS({
67         ...listResultsToDataExplorerItemsMeta(listResults),
68         items: listResults.items.map(resource => resource.uuid),
69     });
70
71 const couldNotFetchWorkflows = () =>
72     snackbarActions.OPEN_SNACKBAR({
73         message: 'Could not fetch workflows.',
74         kind: SnackbarKind.ERROR
75     });