19988: Refactor getOrder into data explorer middleware when no order prefixes are...
[arvados-workbench2.git] / 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 { navigateTo } from 'store/navigation/navigation-action';
17 import { updateFavorites } from 'store/favorites/favorites-actions';
18 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
19 import { setBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
20 import { ResourceKind, extractUuidKind } from 'models/resource';
21 import { ownerNameActions } from 'store/owner-name/owner-name-actions';
22 import { getUserDisplayName } from 'models/user';
23 import { CollectionResource } from 'models/collection';
24
25 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
26     constructor(private services: ServiceRepository, id: string) {
27         super(id);
28     }
29
30     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
31         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
32         if (!dataExplorer) {
33             api.dispatch(collectionPanelDataExplorerIsNotSet());
34         } else {
35             try {
36                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
37                 const userUuid = getUserUuid(api.getState());
38                 const pathname = api.getState().router.location!.pathname;
39                 const contentAddress = pathname.split('/')[2];
40                 const response = await this.services.collectionService.list({
41                     limit: dataExplorer.rowsPerPage,
42                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
43                     filters: new FilterBuilder()
44                         .addEqual('portable_data_hash', contentAddress)
45                         .addILike("name", dataExplorer.searchValue)
46                         .getFilters(),
47                     includeOldVersions: true,
48                     order: getOrder<CollectionResource>(dataExplorer)
49                 });
50                 const userUuids = response.items.map(it => {
51                     if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
52                         return it.ownerUuid;
53                     } else {
54                         return '';
55                     }
56                 }
57                 );
58                 const groupUuids = response.items.map(it => {
59                     if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
60                         return it.ownerUuid;
61                     } else {
62                         return '';
63                     }
64                 });
65                 const responseUsers = await this.services.userService.list({
66                     filters: new FilterBuilder()
67                         .addIn('uuid', userUuids)
68                         .getFilters(),
69                     count: "none"
70                 });
71                 const responseGroups = await this.services.groupsService.list({
72                     filters: new FilterBuilder()
73                         .addIn('uuid', groupUuids)
74                         .getFilters(),
75                     count: "none"
76                 });
77                 responseUsers.items.forEach(it => {
78                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({
79                         name: it.uuid === userUuid
80                             ? 'User: Me'
81                             : `User: ${getUserDisplayName(it)}`,
82                         uuid: it.uuid
83                     }));
84                 });
85                 responseGroups.items.forEach(it => {
86                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
87                 });
88                 api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
89                 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
90                 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
91                 if (response.itemsAvailable === 1) {
92                     api.dispatch<any>(navigateTo(response.items[0].uuid));
93                     api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
94                 } else {
95                     api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
96                     api.dispatch(resourcesActions.SET_RESOURCES(response.items));
97                     api.dispatch(collectionsContentAddressActions.SET_ITEMS({
98                         items: response.items.map((resource: any) => resource.uuid),
99                         itemsAvailable: response.itemsAvailable,
100                         page: Math.floor(response.offset / response.limit),
101                         rowsPerPage: response.limit
102                     }));
103                 }
104             } catch (e) {
105                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
106                 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
107                     items: [],
108                     itemsAvailable: 0,
109                     page: 0,
110                     rowsPerPage: dataExplorer.rowsPerPage
111                 }));
112                 api.dispatch(couldNotFetchCollections());
113             }
114         }
115     }
116 }
117
118 const collectionPanelDataExplorerIsNotSet = () =>
119     snackbarActions.OPEN_SNACKBAR({
120         message: 'Collection panel is not ready.',
121         kind: SnackbarKind.ERROR
122     });
123
124 const couldNotFetchCollections = () =>
125     snackbarActions.OPEN_SNACKBAR({
126         message: 'Could not fetch collection with this content address.',
127         kind: SnackbarKind.ERROR
128     });