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 { SortDirection } from '~/components/data-table/data-column';
14 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
15 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
16 import { FavoritePanelColumnNames } from '~/views/favorite-panel/favorite-panel';
17 import { publicFavoritePanelActions } from '~/store/public-favorites-panel/public-favorites-action';
18 import { DataColumns } from '~/components/data-table/data-table';
19 import { serializeSimpleObjectTypeFilters } from '../resource-type-filters/resource-type-filters';
20 import { LinkResource, LinkClass } from '~/models/link';
21 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
22 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
23 import { updatePublicFavorites } from '~/store/public-favorites/public-favorites-actions';
25 export class PublicFavoritesMiddlewareService extends DataExplorerMiddlewareService {
26 constructor(private services: ServiceRepository, id: string) {
30 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
31 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
33 api.dispatch(favoritesPanelDataExplorerIsNotSet());
35 const columns = dataExplorer.columns as DataColumns<string>;
36 const sortColumn = getSortColumn(dataExplorer);
37 const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
40 const linkOrder = new OrderBuilder<LinkResource>();
41 const contentOrder = new OrderBuilder<GroupContentsResource>();
43 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
44 const direction = sortColumn.sortDirection === SortDirection.ASC
46 : OrderDirection.DESC;
48 linkOrder.addOrder(direction, "name");
50 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
51 .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
52 .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
55 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
56 const uuidPrefix = api.getState().auth.config.uuidPrefix;
57 const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
58 const responseLinks = await this.services.linkService.list({
59 limit: dataExplorer.rowsPerPage,
60 offset: dataExplorer.page * dataExplorer.rowsPerPage,
61 filters: new FilterBuilder()
62 .addEqual('link_class', LinkClass.STAR)
63 .addEqual('owner_uuid', publicProjectUuid)
64 .addIsA("head_uuid", typeFilters)
67 const uuids = responseLinks.items.map(it => it.headUuid);
68 const groupItems: any = await this.services.groupsService.list({
69 filters: new FilterBuilder()
71 .addILike("name", dataExplorer.searchValue)
72 .addIsA("uuid", typeFilters)
75 const collectionItems: any = await this.services.collectionService.list({
76 filters: new FilterBuilder()
78 .addILike("name", dataExplorer.searchValue)
79 .addIsA("uuid", typeFilters)
82 const processItems: any = await this.services.containerRequestService.list({
83 filters: new FilterBuilder()
85 .addILike("name", dataExplorer.searchValue)
86 .addIsA("uuid", typeFilters)
89 const response = groupItems;
90 collectionItems.items.map((it: any) => {
91 response.itemsAvailable++;
92 response.items.push(it);
94 processItems.items.map((it: any) => {
95 response.itemsAvailable++;
96 response.items.push(it);
98 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
99 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
100 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
101 items: response.items.map((resource: any) => resource.uuid),
102 itemsAvailable: response.itemsAvailable,
103 page: Math.floor(response.offset / response.limit),
104 rowsPerPage: response.limit
106 api.dispatch<any>(updatePublicFavorites(response.items.map((item: any) => item.uuid)));
108 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
109 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
113 rowsPerPage: dataExplorer.rowsPerPage
115 api.dispatch(couldNotFetchPublicFavorites());
121 const favoritesPanelDataExplorerIsNotSet = () =>
122 snackbarActions.OPEN_SNACKBAR({
123 message: 'Favorites panel is not ready.',
124 kind: SnackbarKind.ERROR
127 const couldNotFetchPublicFavorites = () =>
128 snackbarActions.OPEN_SNACKBAR({
129 message: 'Could not fetch public favorites contents.',
130 kind: SnackbarKind.ERROR