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';
20 import { navigateTo } from '~/store/navigation/navigation-action';
22 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
23 constructor(private services: ServiceRepository, id: string) {
27 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
28 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
30 api.dispatch(collectionPanelDataExplorerIsNotSet());
32 const sortColumn = getSortColumn(dataExplorer);
34 const contentOrder = new OrderBuilder<GroupContentsResource>();
36 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
37 const direction = sortColumn.sortDirection === SortDirection.ASC
39 : OrderDirection.DESC;
42 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION);
45 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
46 const pathname = api.getState().router.location!.pathname;
47 const contentAddress = pathname.split('/')[2];
48 const response = await this.services.collectionService.list({
49 limit: dataExplorer.rowsPerPage,
50 offset: dataExplorer.page * dataExplorer.rowsPerPage,
51 filters: new FilterBuilder()
52 .addEqual('portableDataHash', contentAddress)
55 if (response.itemsAvailable === 1) {
56 api.dispatch<any>(navigateTo(response.items[0].uuid));
57 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
59 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
60 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
61 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
62 items: response.items.map((resource: any) => resource.uuid),
63 itemsAvailable: response.itemsAvailable,
64 page: Math.floor(response.offset / response.limit),
65 rowsPerPage: response.limit
69 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
70 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
74 rowsPerPage: dataExplorer.rowsPerPage
76 api.dispatch(couldNotFetchCollections());
82 const collectionPanelDataExplorerIsNotSet = () =>
83 snackbarActions.OPEN_SNACKBAR({
84 message: 'Collection panel is not ready.',
85 kind: SnackbarKind.ERROR
88 const couldNotFetchCollections = () =>
89 snackbarActions.OPEN_SNACKBAR({
90 message: 'Could not fetch collection with this content address.',
91 kind: SnackbarKind.ERROR