21288: new api request if wf is not in top 100 list Arvados-DCO-1.1-Signed-off-by...
[arvados.git] / services / workbench2 / src / store / collections-content-address-panel / collections-content-address-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
25
26 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
27     constructor(private services: ServiceRepository, id: string) {
28         super(id);
29     }
30
31     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
32         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
33         if (!dataExplorer) {
34             api.dispatch(collectionPanelDataExplorerIsNotSet());
35         } else {
36             try {
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)
47                         .getFilters(),
48                     includeOldVersions: true,
49                     order: getOrder<CollectionResource>(dataExplorer)
50                 });
51                 const userUuids = response.items.map(it => {
52                     if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
53                         return it.ownerUuid;
54                     } else {
55                         return '';
56                     }
57                 }
58                 );
59                 const groupUuids = response.items.map(it => {
60                     if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
61                         return it.ownerUuid;
62                     } else {
63                         return '';
64                     }
65                 });
66                 const responseUsers = await this.services.userService.list({
67                     filters: new FilterBuilder()
68                         .addIn('uuid', userUuids)
69                         .getFilters(),
70                     count: "none"
71                 });
72                 const responseGroups = await this.services.groupsService.list({
73                     filters: new FilterBuilder()
74                         .addIn('uuid', groupUuids)
75                         .getFilters(),
76                     count: "none"
77                 });
78                 responseUsers.items.forEach(it => {
79                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({
80                         name: it.uuid === userUuid
81                             ? 'User: Me'
82                             : `User: ${getUserDisplayName(it)}`,
83                         uuid: it.uuid
84                     }));
85                 });
86                 responseGroups.items.forEach(it => {
87                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
88                 });
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()));
95                 } else {
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
103                     }));
104                 }
105             } catch (e) {
106                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
107                 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
108                     items: [],
109                     itemsAvailable: 0,
110                     page: 0,
111                     rowsPerPage: dataExplorer.rowsPerPage
112                 }));
113                 api.dispatch(couldNotFetchCollections());
114             }
115         }
116     }
117 }
118
119 const collectionPanelDataExplorerIsNotSet = () =>
120     snackbarActions.OPEN_SNACKBAR({
121         message: 'Collection panel is not ready.',
122         kind: SnackbarKind.ERROR
123     });
124
125 const couldNotFetchCollections = () =>
126     snackbarActions.OPEN_SNACKBAR({
127         message: 'Could not fetch collection with this content address.',
128         kind: SnackbarKind.ERROR
129     });