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