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 { GroupsPanelColumnNames } from 'views/groups-panel/groups-panel';
18 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
20 export class GroupsPanelMiddlewareService extends DataExplorerMiddlewareService {
21 constructor(private services: ServiceRepository, id: string) {
24 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
25 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
27 api.dispatch(groupsPanelDataExplorerIsNotSet());
30 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
31 const order = new OrderBuilder<GroupResource>();
32 const sortColumn = getSortColumn(dataExplorer);
35 sortColumn.sortDirection === SortDirection.ASC && sortColumn.name === GroupsPanelColumnNames.GROUP
37 : OrderDirection.DESC;
38 order.addOrder(direction, 'name');
40 const filters = new FilterBuilder()
41 .addEqual('group_class', GroupClass.ROLE)
42 .addILike('name', dataExplorer.searchValue)
44 const response = await this.services.groupsService
46 ...dataExplorerToListParams(dataExplorer),
48 order: order.getOrder(),
50 api.dispatch(updateResources(response.items));
51 api.dispatch(GroupsPanelActions.SET_ITEMS({
52 ...listResultsToDataExplorerItemsMeta(response),
53 items: response.items.map(item => item.uuid),
55 const permissions = await this.services.permissionService.list({
56 filters: new FilterBuilder()
57 .addIn('head_uuid', response.items.map(item => item.uuid))
60 api.dispatch(updateResources(permissions.items));
62 api.dispatch(couldNotFetchFavoritesContents());
64 api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
70 const groupsPanelDataExplorerIsNotSet = () =>
71 snackbarActions.OPEN_SNACKBAR({
72 message: 'Groups panel is not ready.',
73 kind: SnackbarKind.ERROR
76 const couldNotFetchFavoritesContents = () =>
77 snackbarActions.OPEN_SNACKBAR({
78 message: 'Could not fetch groups.',
79 kind: SnackbarKind.ERROR