Extract function for adding group member
[arvados.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, DataExplorer, getSortColumn } 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 import { OrderBuilder, OrderDirection } from '~/services/api/order-builder';
15 import { GroupResource, GroupClass } from '~/models/group';
16 import { SortDirection } from '~/components/data-table/data-column';
17 import { GroupsPanelColumnNames } from '~/views/groups-panel/groups-panel';
18
19 export class GroupsPanelMiddlewareService extends DataExplorerMiddlewareService {
20
21     constructor(private services: ServiceRepository, id: string) {
22         super(id);
23     }
24
25     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
26
27         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
28
29         if (!dataExplorer) {
30
31             api.dispatch(groupsPanelDataExplorerIsNotSet());
32
33         } else {
34
35             try {
36
37                 const order = new OrderBuilder<GroupResource>();
38                 const sortColumn = getSortColumn(dataExplorer);
39                 if (sortColumn) {
40                     const direction =
41                         sortColumn.sortDirection === SortDirection.ASC && sortColumn.name === GroupsPanelColumnNames.GROUP
42                             ? OrderDirection.ASC
43                             : OrderDirection.DESC;
44
45                     order.addOrder(direction, 'name');
46                 }
47
48                 const filters = new FilterBuilder()
49                     .addNotIn('groupClass', [GroupClass.PROJECT])
50                     .addILike('name', dataExplorer.searchValue)
51                     .getFilters();
52
53                 const response = await this.services.groupsService
54                     .list({
55                         ...dataExplorerToListParams(dataExplorer),
56                         filters,
57                         order: order.getOrder(),
58                     });
59
60                 api.dispatch(updateResources(response.items));
61
62                 api.dispatch(GroupsPanelActions.SET_ITEMS({
63                     ...listResultsToDataExplorerItemsMeta(response),
64                     items: response.items.map(item => item.uuid),
65                 }));
66
67                 const permissions = await this.services.permissionService.list({
68
69                     filters: new FilterBuilder()
70                         .addIn('headUuid', response.items.map(item => item.uuid))
71                         .getFilters()
72
73                 });
74
75                 api.dispatch(updateResources(permissions.items));
76
77
78             } catch (e) {
79
80                 api.dispatch(couldNotFetchFavoritesContents());
81
82             }
83         }
84     }
85 }
86
87 const groupsPanelDataExplorerIsNotSet = () =>
88     snackbarActions.OPEN_SNACKBAR({
89         message: 'Groups panel is not ready.'
90     });
91
92 const couldNotFetchFavoritesContents = () =>
93     snackbarActions.OPEN_SNACKBAR({
94         message: 'Could not fetch groups.',
95         kind: SnackbarKind.ERROR
96     });
97