18123: Add tests for hide group member and rename/delete group.
[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, getDataExplorerColumnFilters } 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 { OrderDirection, OrderBuilder } from 'services/api/order-builder';
14 import { GroupContentsResource, GroupContentsResourcePrefix } from "services/groups-service/groups-service";
15 import { ListResults } from 'services/common-service/common-service';
16 import { searchResultsPanelActions } from 'store/search-results-panel/search-results-panel-actions';
17 import {
18     getSearchSessions,
19     queryToFilters,
20     getAdvancedDataFromQuery
21 } from 'store/search-bar/search-bar-actions';
22 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
23 import { joinFilters } from 'services/api/filter-builder';
24 import { DataColumns } from 'components/data-table/data-table';
25 import { serializeResourceTypeFilters } from 'store//resource-type-filters/resource-type-filters';
26 import { ProjectPanelColumnNames } from 'views/project-panel/project-panel';
27 import { Resource } from 'models/resource';
28
29 export class SearchResultsMiddlewareService extends DataExplorerMiddlewareService {
30     constructor(private services: ServiceRepository, id: string) {
31         super(id);
32     }
33
34     async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean) {
35         const state = api.getState();
36         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
37         const searchValue = state.searchBar.searchValue;
38         const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue);
39         const sessions = getSearchSessions(clusterId, state.auth.sessions);
40
41         if (searchValue.trim() === '') {
42             return;
43         }
44
45         const initial = {
46             itemsAvailable: 0,
47             items: [] as GroupContentsResource[],
48             kind: '',
49             offset: 0,
50             limit: 10
51         };
52
53         if (criteriaChanged) {
54             api.dispatch(setItems(initial));
55         }
56
57         sessions.forEach(session => {
58             const params = getParams(dataExplorer, searchValue, session.apiRevision);
59             this.services.groupsService.contents('', params, session)
60                 .then((response) => {
61                     api.dispatch(updateResources(response.items));
62                     api.dispatch(appendItems(response));
63                 }).catch(() => {
64                     api.dispatch(couldNotFetchSearchResults(session.clusterId));
65                 });
66             }
67         );
68     }
69 }
70
71 const typeFilters = (columns: DataColumns<string>) => serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
72
73 export const getParams = (dataExplorer: DataExplorer, query: string, apiRevision: number) => ({
74     ...dataExplorerToListParams(dataExplorer),
75     filters: joinFilters(
76         queryToFilters(query, apiRevision),
77         typeFilters(dataExplorer.columns)
78     ),
79     order: getOrder(dataExplorer),
80     includeTrash: getAdvancedDataFromQuery(query).inTrash,
81     includeOldVersions: getAdvancedDataFromQuery(query).pastVersions
82 });
83
84 const getOrder = (dataExplorer: DataExplorer) => {
85     const sortColumn = getSortColumn(dataExplorer);
86     const order = new OrderBuilder<GroupContentsResource>();
87     if (sortColumn) {
88         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
89             ? OrderDirection.ASC
90             : OrderDirection.DESC;
91
92         return order
93             .addOrder(sortDirection, sortColumn.name as keyof Resource, GroupContentsResourcePrefix.COLLECTION)
94             .addOrder(sortDirection, sortColumn.name as keyof Resource, GroupContentsResourcePrefix.PROCESS)
95             .addOrder(sortDirection, sortColumn.name as keyof Resource, GroupContentsResourcePrefix.PROJECT)
96             .getOrder();
97     } else {
98         return order.getOrder();
99     }
100 };
101
102 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
103     searchResultsPanelActions.SET_ITEMS({
104         ...listResultsToDataExplorerItemsMeta(listResults),
105         items: listResults.items.map(resource => resource.uuid),
106     });
107
108 export const appendItems = (listResults: ListResults<GroupContentsResource>) =>
109     searchResultsPanelActions.APPEND_ITEMS({
110         ...listResultsToDataExplorerItemsMeta(listResults),
111         items: listResults.items.map(resource => resource.uuid),
112     });
113
114 const couldNotFetchSearchResults = (cluster: string) =>
115     snackbarActions.OPEN_SNACKBAR({
116         message: `Could not fetch search results from ${cluster}.`,
117         kind: SnackbarKind.ERROR
118     });