17782: Merge branch 'main' into 17782-react-scripts-ts-migration
[arvados.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         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
24         const groupUuid = getCurrentGroupDetailsPanelUuid(api.getState().properties);
25         if (!dataExplorer || !groupUuid) {
26             api.dispatch(groupsDetailsPanelDataExplorerIsNotSet());
27         } else {
28             try {
29                 const permissions = await this.services.permissionService.list({
30                     filters: new FilterBuilder()
31                         .addEqual('tail_uuid', groupUuid)
32                         .addEqual('link_class', LinkClass.PERMISSION)
33                         .getFilters()
34                 });
35                 api.dispatch(updateResources(permissions.items));
36                 const users = await this.services.userService.list({
37                     filters: new FilterBuilder()
38                         .addIn('uuid', permissions.items.map(item => item.headUuid))
39                         .getFilters(),
40                     count: "none"
41                 });
42                 api.dispatch(GroupDetailsPanelActions.SET_ITEMS({
43                     ...listResultsToDataExplorerItemsMeta(permissions),
44                     items: users.items.map(item => item.uuid),
45                 }));
46                 api.dispatch(updateResources(users.items));
47             } catch (e) {
48                 api.dispatch(couldNotFetchGroupDetailsContents());
49             }
50         }
51     }
52 }
53
54 const groupsDetailsPanelDataExplorerIsNotSet = () =>
55     snackbarActions.OPEN_SNACKBAR({
56         message: 'Group details panel is not ready.',
57         kind: SnackbarKind.ERROR
58     });
59
60 const couldNotFetchGroupDetailsContents = () =>
61     snackbarActions.OPEN_SNACKBAR({
62         message: 'Could not fetch group details.',
63         kind: SnackbarKind.ERROR
64     });