Filter groups by name
[arvados-workbench2.git] / src / store / groups-panel / groups-panel-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch, MiddlewareAPI } from "redux";
6 import { DataExplorerMiddlewareService, listResultsToDataExplorerItemsMeta, dataExplorerToListParams } from "~/store/data-explorer/data-explorer-middleware-service";
7 import { RootState } from "~/store/store";
8 import { ServiceRepository } from "~/services/services";
9 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
10 import { getDataExplorer } from "~/store/data-explorer/data-explorer-reducer";
11 import { GroupsPanelActions } from '~/store/groups-panel/groups-panel-actions';
12 import { FilterBuilder } from '~/services/api/filter-builder';
13 import { updateResources } from '~/store/resources/resources-actions';
14
15 export class GroupsPanelMiddlewareService extends DataExplorerMiddlewareService {
16
17     constructor(private services: ServiceRepository, id: string) {
18         super(id);
19     }
20
21     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
22
23         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
24
25         if (!dataExplorer) {
26
27             api.dispatch(groupsPanelDataExplorerIsNotSet());
28
29         } else {
30
31             try {
32
33                 const filters = new FilterBuilder()
34                     .addEqual('groupClass', null)
35                     .addILike('name', dataExplorer.searchValue)
36                     .getFilters();
37
38                 const response = await this.services.groupsService
39                     .list({
40                         ...dataExplorerToListParams(dataExplorer),
41                         filters,
42                     });
43
44                 api.dispatch(updateResources(response.items));
45
46                 api.dispatch(GroupsPanelActions.SET_ITEMS({
47                     ...listResultsToDataExplorerItemsMeta(response),
48                     items: response.items.map(item => item.uuid),
49                 }));
50
51
52             } catch (e) {
53
54                 api.dispatch(couldNotFetchFavoritesContents());
55
56             }
57         }
58     }
59 }
60
61 const groupsPanelDataExplorerIsNotSet = () =>
62     snackbarActions.OPEN_SNACKBAR({
63         message: 'Groups panel is not ready.'
64     });
65
66 const couldNotFetchFavoritesContents = () =>
67     snackbarActions.OPEN_SNACKBAR({
68         message: 'Could not fetch groups.',
69         kind: SnackbarKind.ERROR
70     });