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