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, dataExplorerToListParams, 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 { DataExplorer, 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';
25 import { ListArguments, ListResults } from 'services/common-service/common-service';
26 import { couldNotFetchItemsAvailable } from 'store/data-explorer/data-explorer-action';
28 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
29 constructor(private services: ServiceRepository, id: string) {
33 async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
34 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
36 api.dispatch(collectionPanelDataExplorerIsNotSet());
39 if (!background) { api.dispatch(progressIndicatorActions.START_WORKING(this.getId())); }
41 const state = api.getState();
42 const userUuid = getUserUuid(state);
43 const pathname = state.router.location!.pathname;
44 const contentAddress = pathname.split('/')[2];
47 const response = await this.services.collectionService.list(getParams(dataExplorer, contentAddress));
48 const userUuids = response.items.map(it => {
49 if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
56 const groupUuids = response.items.map(it => {
57 if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
63 const responseUsers = await this.services.userService.list({
64 filters: new FilterBuilder()
65 .addIn('uuid', userUuids)
69 const responseGroups = await this.services.groupsService.list({
70 filters: new FilterBuilder()
71 .addIn('uuid', groupUuids)
75 responseUsers.items.forEach(it => {
76 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({
77 name: it.uuid === userUuid
79 : `User: ${getUserDisplayName(it)}`,
83 responseGroups.items.forEach(it => {
84 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
86 api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
87 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
88 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
89 if (response.itemsAvailable === 1) {
90 api.dispatch<any>(replace(getNavUrl(response.items[0].uuid, api.getState().auth)));
92 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
93 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
94 items: response.items.map((resource: any) => resource.uuid),
95 itemsAvailable: response.itemsAvailable,
96 page: Math.floor(response.offset / response.limit),
97 rowsPerPage: response.limit
101 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
105 rowsPerPage: dataExplorer.rowsPerPage
107 api.dispatch(couldNotFetchCollections());
109 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
114 async requestCount(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
115 const state = api.getState();
116 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
117 const pathname = state.router.location!.pathname;
118 const contentAddress = pathname.split('/')[2];
120 if (criteriaChanged) {
121 // Get itemsAvailable
122 return this.services.collectionService.list(getCountParams(dataExplorer, contentAddress))
123 .then((results: ListResults<CollectionResource>) => {
124 if (results.itemsAvailable !== undefined) {
125 api.dispatch<any>(collectionsContentAddressActions.SET_ITEMS_AVAILABLE(results.itemsAvailable));
127 couldNotFetchItemsAvailable();
134 const getFilters = (dataExplorer: DataExplorer, contentAddress: string) => (
136 .addEqual('portable_data_hash', contentAddress)
137 .addILike("name", dataExplorer.searchValue)
141 const getParams = (dataExplorer: DataExplorer, contentAddress: string): ListArguments => ({
142 ...dataExplorerToListParams(dataExplorer),
143 filters: getFilters(dataExplorer, contentAddress),
144 order: getOrder<CollectionResource>(dataExplorer),
145 includeOldVersions: true,
149 const getCountParams = (dataExplorer: DataExplorer, contentAddress: string): ListArguments => ({
152 filters: getFilters(dataExplorer, contentAddress),
153 includeOldVersions: true,
156 const collectionPanelDataExplorerIsNotSet = () =>
157 snackbarActions.OPEN_SNACKBAR({
158 message: 'Collection panel is not ready.',
159 kind: SnackbarKind.ERROR
162 const couldNotFetchCollections = () =>
163 snackbarActions.OPEN_SNACKBAR({
164 message: 'Could not fetch collection with this content address.',
165 kind: SnackbarKind.ERROR