1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { ServiceRepository } from '~/services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService } from '~/store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from '~/store/store';
9 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
10 import { getDataExplorer } from '~/store/data-explorer/data-explorer-reducer';
11 import { resourcesActions } from '~/store/resources/resources-actions';
12 import { FilterBuilder } from '~/services/api/filter-builder';
13 import { SortDirection } from '~/components/data-table/data-column';
14 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
15 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
16 import { FavoritePanelColumnNames } from '~/views/favorite-panel/favorite-panel';
17 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
18 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
19 import { collectionsContentAddressActions } from './collections-content-address-panel-actions';
21 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
22 constructor(private services: ServiceRepository, id: string) {
26 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
27 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
29 api.dispatch(collectionPanelDataExplorerIsNotSet());
31 const sortColumn = getSortColumn(dataExplorer);
33 const contentOrder = new OrderBuilder<GroupContentsResource>();
35 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
36 const direction = sortColumn.sortDirection === SortDirection.ASC
38 : OrderDirection.DESC;
41 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION);
44 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
45 const pathname = api.getState().router.location!.pathname;
46 const contentAddress = pathname.split('/')[2];
47 const response = await this.services.collectionService.list({
48 limit: dataExplorer.rowsPerPage,
49 offset: dataExplorer.page * dataExplorer.rowsPerPage,
50 filters: new FilterBuilder()
51 .addEqual('portableDataHash', contentAddress)
54 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
55 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
56 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
57 items: response.items.map((resource: any) => resource.uuid),
58 itemsAvailable: response.itemsAvailable,
59 page: Math.floor(response.offset / response.limit),
60 rowsPerPage: response.limit
63 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
64 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
68 rowsPerPage: dataExplorer.rowsPerPage
70 api.dispatch(couldNotFetchCollections());
76 const collectionPanelDataExplorerIsNotSet = () =>
77 snackbarActions.OPEN_SNACKBAR({
78 message: 'Collection panel is not ready.',
79 kind: SnackbarKind.ERROR
82 const couldNotFetchCollections = () =>
83 snackbarActions.OPEN_SNACKBAR({
84 message: 'Could not fetch collection with this content address.',
85 kind: SnackbarKind.ERROR