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, getOrder } from 'store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from 'store/store';
9 import { getUserUuid } from "common/getuser";
10 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
11 import { getDataExplorer } from 'store/data-explorer/data-explorer-reducer';
12 import { resourcesActions } from 'store/resources/resources-actions';
13 import { FilterBuilder } from 'services/api/filter-builder';
14 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
15 import { collectionsContentAddressActions } from './collections-content-address-panel-actions';
16 import { updateFavorites } from 'store/favorites/favorites-actions';
17 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
18 import { setBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
19 import { ResourceKind, extractUuidKind } from 'models/resource';
20 import { ownerNameActions } from 'store/owner-name/owner-name-actions';
21 import { getUserDisplayName } from 'models/user';
22 import { CollectionResource } from 'models/collection';
23 import { replace } from "react-router-redux";
24 import { getNavUrl } from 'routes/routes';
26 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
27 constructor(private services: ServiceRepository, id: string) {
31 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
32 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
34 api.dispatch(collectionPanelDataExplorerIsNotSet());
37 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
38 const userUuid = getUserUuid(api.getState());
39 const pathname = api.getState().router.location!.pathname;
40 const contentAddress = pathname.split('/')[2];
41 const response = await this.services.collectionService.list({
42 limit: dataExplorer.rowsPerPage,
43 offset: dataExplorer.page * dataExplorer.rowsPerPage,
44 filters: new FilterBuilder()
45 .addEqual('portable_data_hash', contentAddress)
46 .addILike("name", dataExplorer.searchValue)
48 includeOldVersions: true,
49 order: getOrder<CollectionResource>(dataExplorer)
51 const userUuids = response.items.map(it => {
52 if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
59 const groupUuids = response.items.map(it => {
60 if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
66 const responseUsers = await this.services.userService.list({
67 filters: new FilterBuilder()
68 .addIn('uuid', userUuids)
72 const responseGroups = await this.services.groupsService.list({
73 filters: new FilterBuilder()
74 .addIn('uuid', groupUuids)
78 responseUsers.items.forEach(it => {
79 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({
80 name: it.uuid === userUuid
82 : `User: ${getUserDisplayName(it)}`,
86 responseGroups.items.forEach(it => {
87 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
89 api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
90 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
91 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
92 if (response.itemsAvailable === 1) {
93 api.dispatch<any>(replace(getNavUrl(response.items[0].uuid, api.getState().auth)));
94 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
96 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
97 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
98 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
99 items: response.items.map((resource: any) => resource.uuid),
100 itemsAvailable: response.itemsAvailable,
101 page: Math.floor(response.offset / response.limit),
102 rowsPerPage: response.limit
106 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
107 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
111 rowsPerPage: dataExplorer.rowsPerPage
113 api.dispatch(couldNotFetchCollections());
119 const collectionPanelDataExplorerIsNotSet = () =>
120 snackbarActions.OPEN_SNACKBAR({
121 message: 'Collection panel is not ready.',
122 kind: SnackbarKind.ERROR
125 const couldNotFetchCollections = () =>
126 snackbarActions.OPEN_SNACKBAR({
127 message: 'Could not fetch collection with this content address.',
128 kind: SnackbarKind.ERROR