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';
20 export class SearchResultsMiddlewareService extends DataExplorerMiddlewareService {
21 constructor(private services: ServiceRepository, id: string) {
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;
31 const response = await this.services.groupsService.contents(userUuid, getParams(dataExplorer, searchValue));
32 api.dispatch(updateResources(response.items));
33 api.dispatch(setItems(response));
35 api.dispatch(couldNotFetchWorkflows());
40 export const getParams = (dataExplorer: DataExplorer, searchValue: string) => ({
41 ...dataExplorerToListParams(dataExplorer),
42 filters: getFilters('name', searchValue),
43 order: getOrder(dataExplorer)
46 const getOrder = (dataExplorer: DataExplorer) => {
47 const sortColumn = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
48 const order = new OrderBuilder<GroupContentsResource>();
50 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
52 : OrderDirection.DESC;
54 const columnName = sortColumn && sortColumn.name === SearchResultsPanelColumnNames.NAME ? "name" : "modifiedAt";
56 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
57 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
58 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
61 return order.getOrder();
65 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
66 searchResultsPanelActions.SET_ITEMS({
67 ...listResultsToDataExplorerItemsMeta(listResults),
68 items: listResults.items.map(resource => resource.uuid),
71 const couldNotFetchWorkflows = () =>
72 snackbarActions.OPEN_SNACKBAR({
73 message: 'Could not fetch workflows.',
74 kind: SnackbarKind.ERROR