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, 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, 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 { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
19 export class GroupsPanelMiddlewareService extends DataExplorerMiddlewareService {
20 constructor(private services: ServiceRepository, id: string) {
23 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
24 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
26 api.dispatch(groupsPanelDataExplorerIsNotSet());
29 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
30 const sortColumn = getSortColumn<GroupResource>(dataExplorer);
31 const order = new OrderBuilder<GroupResource>();
32 if (sortColumn && sortColumn.sort) {
34 sortColumn.sort.direction === SortDirection.ASC
36 : OrderDirection.DESC;
37 order.addOrder(direction, sortColumn.sort.field);
39 const filters = new FilterBuilder()
40 .addEqual('group_class', GroupClass.ROLE)
41 .addILike('name', dataExplorer.searchValue)
43 const groups = await this.services.groupsService
45 ...dataExplorerToListParams(dataExplorer),
47 order: order.getOrder(),
49 api.dispatch(updateResources(groups.items));
50 api.dispatch(GroupsPanelActions.SET_ITEMS({
51 ...listResultsToDataExplorerItemsMeta(groups),
52 items: groups.items.map(item => item.uuid),
55 // Get group member count
56 groups.items.map(group => (
57 this.services.permissionService.list({
59 filters: new FilterBuilder()
60 .addEqual('head_uuid', group.uuid)
63 api.dispatch(updateResources([{
65 memberCount: members.itemsAvailable,
66 } as GroupResource]));
68 // In case of error, store null to stop spinners and show failure icon
69 api.dispatch(updateResources([{
72 } as GroupResource]));
76 api.dispatch(couldNotFetchGroupList());
78 api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
84 const groupsPanelDataExplorerIsNotSet = () =>
85 snackbarActions.OPEN_SNACKBAR({
86 message: 'Groups panel is not ready.',
87 kind: SnackbarKind.ERROR
90 const couldNotFetchGroupList = () =>
91 snackbarActions.OPEN_SNACKBAR({
92 message: 'Could not fetch groups.',
93 kind: SnackbarKind.ERROR