1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
21 export class SearchResultsMiddlewareService extends DataExplorerMiddlewareService {
22 constructor(private services: ServiceRepository, id: string) {
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;
32 const response = await this.services.groupsService.contents('', getParams(dataExplorer, searchValue));
33 api.dispatch(updateResources(response.items));
34 api.dispatch(setItems(response));
36 api.dispatch(couldNotFetchWorkflows());
41 export const getParams = (dataExplorer: DataExplorer, searchValue: string) => ({
42 ...dataExplorerToListParams(dataExplorer),
43 filters: getFilters('name', searchValue, {}),
44 order: getOrder(dataExplorer)
47 const getOrder = (dataExplorer: DataExplorer) => {
48 const sortColumn = getSortColumn(dataExplorer);
49 const order = new OrderBuilder<GroupContentsResource>();
51 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
53 : OrderDirection.DESC;
55 const columnName = sortColumn && sortColumn.name === SearchResultsPanelColumnNames.NAME ? "name" : "modifiedAt";
57 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
58 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
59 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
62 return order.getOrder();
66 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
67 searchResultsPanelActions.SET_ITEMS({
68 ...listResultsToDataExplorerItemsMeta(listResults),
69 items: listResults.items.map(resource => resource.uuid),
72 const couldNotFetchWorkflows = () =>
73 snackbarActions.OPEN_SNACKBAR({
74 message: 'Could not fetch workflows.',
75 kind: SnackbarKind.ERROR