21910: Merge branch 'main' into 21910-remove-api_client_id
[arvados.git] / services / workbench2 / src / store / public-favorites-panel / public-favorites-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, getDataExplorerColumnFilters } 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 { FavoritePanelColumnNames } from 'views/favorite-panel/favorite-panel';
14 import { publicFavoritePanelActions } from 'store/public-favorites-panel/public-favorites-action';
15 import { DataColumns } from 'components/data-table/data-table';
16 import { serializeSimpleObjectTypeFilters } from '../resource-type-filters/resource-type-filters';
17 import { LinkClass } from 'models/link';
18 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
19 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
20 import { GroupContentsResource } from 'services/groups-service/groups-service';
21
22 export class PublicFavoritesMiddlewareService extends DataExplorerMiddlewareService {
23     constructor(private services: ServiceRepository, id: string) {
24         super(id);
25     }
26
27     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
28         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
29         if (!dataExplorer) {
30             api.dispatch(favoritesPanelDataExplorerIsNotSet());
31         } else {
32             const columns = dataExplorer.columns as DataColumns<string, GroupContentsResource>;
33             const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
34
35             try {
36                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
37                 const uuidPrefix = api.getState().auth.config.uuidPrefix;
38                 const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
39                 const responseLinks = await this.services.linkService.list({
40                     limit: dataExplorer.rowsPerPage,
41                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
42                     filters: new FilterBuilder()
43                         .addEqual('link_class', LinkClass.STAR)
44                         .addEqual('owner_uuid', publicProjectUuid)
45                         .addIsA("head_uuid", typeFilters)
46                         .getFilters()
47                 });
48                 const uuids = responseLinks.items.map(it => it.headUuid);
49                 const groupItems: any = await this.services.groupsService.list({
50                     filters: new FilterBuilder()
51                         .addIn("uuid", uuids)
52                         .addILike("name", dataExplorer.searchValue)
53                         .addIsA("uuid", typeFilters)
54                         .getFilters()
55                 });
56                 const collectionItems: any = await this.services.collectionService.list({
57                     filters: new FilterBuilder()
58                         .addIn("uuid", uuids)
59                         .addILike("name", dataExplorer.searchValue)
60                         .addIsA("uuid", typeFilters)
61                         .getFilters()
62                 });
63                 const processItems: any = await this.services.containerRequestService.list({
64                     filters: new FilterBuilder()
65                         .addIn("uuid", uuids)
66                         .addILike("name", dataExplorer.searchValue)
67                         .addIsA("uuid", typeFilters)
68                         .getFilters()
69                 });
70                 const response = groupItems;
71                 collectionItems.items.forEach((it: any) => {
72                     response.itemsAvailable++;
73                     response.items.push(it);
74                 });
75                 processItems.items.forEach((it: any) => {
76                     response.itemsAvailable++;
77                     response.items.push(it);
78                 });
79                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
80                 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
81                 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
82                     items: response.items.map((resource: any) => resource.uuid),
83                     itemsAvailable: response.itemsAvailable,
84                     page: Math.floor(response.offset / response.limit),
85                     rowsPerPage: response.limit
86                 }));
87                 api.dispatch<any>(updatePublicFavorites(response.items.map((item: any) => item.uuid)));
88             } catch (e) {
89                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
90                 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
91                     items: [],
92                     itemsAvailable: 0,
93                     page: 0,
94                     rowsPerPage: dataExplorer.rowsPerPage
95                 }));
96                 api.dispatch(couldNotFetchPublicFavorites());
97             }
98         }
99     }
100 }
101
102 const favoritesPanelDataExplorerIsNotSet = () =>
103     snackbarActions.OPEN_SNACKBAR({
104         message: 'Favorites panel is not ready.',
105         kind: SnackbarKind.ERROR
106     });
107
108 const couldNotFetchPublicFavorites = () =>
109     snackbarActions.OPEN_SNACKBAR({
110         message: 'Could not fetch public favorites contents.',
111         kind: SnackbarKind.ERROR
112     });