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';
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 const order = new OrderBuilder<GroupResource>();
30 const sortColumn = getSortColumn(dataExplorer);
33 sortColumn.sortDirection === SortDirection.ASC && sortColumn.name === GroupsPanelColumnNames.GROUP
35 : OrderDirection.DESC;
36 order.addOrder(direction, 'name');
38 const filters = new FilterBuilder()
39 .addNotIn('group_class', [GroupClass.PROJECT])
40 .addILike('name', dataExplorer.searchValue)
42 const response = await this.services.groupsService
44 ...dataExplorerToListParams(dataExplorer),
46 order: order.getOrder(),
48 api.dispatch(updateResources(response.items));
49 api.dispatch(GroupsPanelActions.SET_ITEMS({
50 ...listResultsToDataExplorerItemsMeta(response),
51 items: response.items.map(item => item.uuid),
53 const permissions = await this.services.permissionService.list({
54 filters: new FilterBuilder()
55 .addIn('tail_uuid', response.items.map(item => item.uuid))
58 api.dispatch(updateResources(permissions.items));
60 api.dispatch(couldNotFetchFavoritesContents());
66 const groupsPanelDataExplorerIsNotSet = () =>
67 snackbarActions.OPEN_SNACKBAR({
68 message: 'Groups panel is not ready.',
69 kind: SnackbarKind.ERROR
72 const couldNotFetchFavoritesContents = () =>
73 snackbarActions.OPEN_SNACKBAR({
74 message: 'Could not fetch groups.',
75 kind: SnackbarKind.ERROR