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, DataExplorer, 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 {
21 constructor(private services: ServiceRepository, id: string) {
25 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
27 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
31 api.dispatch(groupsPanelDataExplorerIsNotSet());
37 const order = new OrderBuilder<GroupResource>();
38 const sortColumn = getSortColumn(dataExplorer);
41 sortColumn.sortDirection === SortDirection.ASC && sortColumn.name === GroupsPanelColumnNames.GROUP
43 : OrderDirection.DESC;
45 order.addOrder(direction, 'name');
48 const filters = new FilterBuilder()
49 .addNotIn('groupClass', [GroupClass.PROJECT])
50 .addILike('name', dataExplorer.searchValue)
53 const response = await this.services.groupsService
55 ...dataExplorerToListParams(dataExplorer),
57 order: order.getOrder(),
60 api.dispatch(updateResources(response.items));
62 api.dispatch(GroupsPanelActions.SET_ITEMS({
63 ...listResultsToDataExplorerItemsMeta(response),
64 items: response.items.map(item => item.uuid),
67 const permissions = await this.services.permissionService.list({
69 filters: new FilterBuilder()
70 .addIn('headUuid', response.items.map(item => item.uuid))
75 api.dispatch(updateResources(permissions.items));
80 api.dispatch(couldNotFetchFavoritesContents());
87 const groupsPanelDataExplorerIsNotSet = () =>
88 snackbarActions.OPEN_SNACKBAR({
89 message: 'Groups panel is not ready.'
92 const couldNotFetchFavoritesContents = () =>
93 snackbarActions.OPEN_SNACKBAR({
94 message: 'Could not fetch groups.',
95 kind: SnackbarKind.ERROR