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, 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';
22 export class PublicFavoritesMiddlewareService extends DataExplorerMiddlewareService {
23 constructor(private services: ServiceRepository, id: string) {
27 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
28 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
30 api.dispatch(favoritesPanelDataExplorerIsNotSet());
32 const columns = dataExplorer.columns as DataColumns<string, GroupContentsResource>;
33 const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
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)
48 const uuids = responseLinks.items.map(it => it.headUuid);
49 const groupItems: any = await this.services.groupsService.list({
50 filters: new FilterBuilder()
52 .addILike("name", dataExplorer.searchValue)
53 .addIsA("uuid", typeFilters)
56 const collectionItems: any = await this.services.collectionService.list({
57 filters: new FilterBuilder()
59 .addILike("name", dataExplorer.searchValue)
60 .addIsA("uuid", typeFilters)
63 const processItems: any = await this.services.containerRequestService.list({
64 filters: new FilterBuilder()
66 .addILike("name", dataExplorer.searchValue)
67 .addIsA("uuid", typeFilters)
70 const response = groupItems;
71 collectionItems.items.forEach((it: any) => {
72 response.itemsAvailable++;
73 response.items.push(it);
75 processItems.items.forEach((it: any) => {
76 response.itemsAvailable++;
77 response.items.push(it);
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
87 api.dispatch<any>(updatePublicFavorites(response.items.map((item: any) => item.uuid)));
89 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
90 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
94 rowsPerPage: dataExplorer.rowsPerPage
96 api.dispatch(couldNotFetchPublicFavorites());
102 const favoritesPanelDataExplorerIsNotSet = () =>
103 snackbarActions.OPEN_SNACKBAR({
104 message: 'Favorites panel is not ready.',
105 kind: SnackbarKind.ERROR
108 const couldNotFetchPublicFavorites = () =>
109 snackbarActions.OPEN_SNACKBAR({
110 message: 'Could not fetch public favorites contents.',
111 kind: SnackbarKind.ERROR