Merge branch '14604-ui-improvements'
[arvados-workbench2.git] / src / store / group-details-panel / group-details-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 } 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 { FilterBuilder } from '~/services/api/filter-builder';
12 import { updateResources } from '~/store/resources/resources-actions';
13 import { getCurrentGroupDetailsPanelUuid, GroupDetailsPanelActions } from '~/store/group-details-panel/group-details-panel-actions';
14 import { LinkClass } from '~/models/link';
15
16 export class GroupDetailsPanelMiddlewareService extends DataExplorerMiddlewareService {
17
18     constructor(private services: ServiceRepository, id: string) {
19         super(id);
20     }
21
22     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
23
24         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
25         const groupUuid = getCurrentGroupDetailsPanelUuid(api.getState().properties);
26
27         if (!dataExplorer || !groupUuid) {
28
29             api.dispatch(groupsDetailsPanelDataExplorerIsNotSet());
30
31         } else {
32
33             try {
34
35                 const permissions = await this.services.permissionService.list({
36
37                     filters: new FilterBuilder()
38                         .addEqual('tailUuid', groupUuid)
39                         .addEqual('linkClass', LinkClass.PERMISSION)
40                         .getFilters()
41
42                 });
43
44                 api.dispatch(updateResources(permissions.items));
45
46                 const users = await this.services.userService.list({
47
48                     filters: new FilterBuilder()
49                         .addIn('uuid', permissions.items.map(item => item.headUuid))
50                         .getFilters()
51
52                 });
53
54                 api.dispatch(GroupDetailsPanelActions.SET_ITEMS({
55                     ...listResultsToDataExplorerItemsMeta(permissions),
56                     items: users.items.map(item => item.uuid),
57                 }));
58
59                 api.dispatch(updateResources(users.items));
60
61             } catch (e) {
62
63                 api.dispatch(couldNotFetchGroupDetailsContents());
64
65             }
66         }
67     }
68 }
69
70 const groupsDetailsPanelDataExplorerIsNotSet = () =>
71     snackbarActions.OPEN_SNACKBAR({
72         message: 'Group details panel is not ready.',
73         kind: SnackbarKind.ERROR
74     });
75
76 const couldNotFetchGroupDetailsContents = () =>
77     snackbarActions.OPEN_SNACKBAR({
78         message: 'Could not fetch group details.',
79         kind: SnackbarKind.ERROR
80     });