1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch, MiddlewareAPI } from "redux";
6 import { DataExplorerMiddlewareService, dataExplorerToListParams, 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 { DataExplorer, 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, GroupMembersPanelActions } from 'store/group-details-panel/group-details-panel-actions';
14 import { LinkClass } from 'models/link';
15 import { ResourceKind } from 'models/resource';
16 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
18 export class GroupDetailsPanelMembersMiddlewareService extends DataExplorerMiddlewareService {
20 constructor(private services: ServiceRepository, id: string) {
24 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
25 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
26 const groupUuid = getCurrentGroupDetailsPanelUuid(api.getState().properties);
27 if (!dataExplorer || !groupUuid) {
28 // Noop if data explorer refresh is triggered from another panel
32 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
33 const groupResource = await this.services.groupsService.get(groupUuid);
34 api.dispatch(updateResources([groupResource]));
36 const permissionsIn = await this.services.permissionService.list(getParams(dataExplorer, groupUuid));
37 api.dispatch(updateResources(permissionsIn.items));
39 api.dispatch(GroupMembersPanelActions.SET_ITEMS({
40 ...listResultsToDataExplorerItemsMeta(permissionsIn),
41 items: permissionsIn.items.map(item => item.uuid),
44 const usersIn = await this.services.userService.list({
45 filters: new FilterBuilder()
46 .addIn('uuid', permissionsIn.items
47 .filter((item) => item.tailKind === ResourceKind.USER)
48 .map(item => item.tailUuid))
52 api.dispatch(updateResources(usersIn.items));
54 const projectsIn = await this.services.projectService.list({
55 filters: new FilterBuilder()
56 .addIn('uuid', permissionsIn.items
57 .filter((item) => item.tailKind === ResourceKind.PROJECT)
58 .map(item => item.tailUuid))
62 api.dispatch(updateResources(projectsIn.items));
64 api.dispatch(couldNotFetchGroupDetailsContents());
66 api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
72 export const getParams = (dataExplorer: DataExplorer, groupUuid: string) => ({
73 ...dataExplorerToListParams(dataExplorer),
74 filters: getFilters(groupUuid),
77 export const getFilters = (groupUuid: string) => {
78 const filters = new FilterBuilder()
79 .addEqual('head_uuid', groupUuid)
80 .addEqual('link_class', LinkClass.PERMISSION)
86 const couldNotFetchGroupDetailsContents = () =>
87 snackbarActions.OPEN_SNACKBAR({
88 message: 'Could not fetch group members.',
89 kind: SnackbarKind.ERROR