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 uuid = `${uuidPrefix}-j7d0g-fffffffffffffff`;
58 const responseLinks = await this.services.linkService.list({
59 limit: dataExplorer.rowsPerPage,
60 offset: dataExplorer.page * dataExplorer.rowsPerPage,
61 filters: new FilterBuilder()
62 .addEqual('linkClass', LinkClass.STAR)
63 .addILike("name", dataExplorer.searchValue)
64 .addEqual('ownerUuid', uuid)
65 .addIsA("headUuid", typeFilters)
68 const uuids = responseLinks.items.map(it => it.headUuid);
69 const groupItems: any = await this.services.groupsService.list({
70 filters: new FilterBuilder()
72 .addILike("name", dataExplorer.searchValue)
73 .addIsA("uuid", typeFilters)
76 const collectionItems: any = await this.services.collectionService.list({
77 filters: new FilterBuilder()
79 .addILike("name", dataExplorer.searchValue)
80 .addIsA("uuid", typeFilters)
83 const processItems: any = await this.services.containerRequestService.list({
84 filters: new FilterBuilder()
86 .addILike("name", dataExplorer.searchValue)
87 .addIsA("uuid", typeFilters)
90 const response = groupItems;
91 collectionItems.items.map((it: any) => {
92 response.itemsAvailable++;
93 response.items.push(it);
95 processItems.items.map((it: any) => {
96 response.itemsAvailable++;
97 response.items.push(it);
99 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
100 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
101 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
102 items: response.items.map((resource: any) => resource.uuid),
103 itemsAvailable: response.itemsAvailable,
104 page: Math.floor(response.offset / response.limit),
105 rowsPerPage: response.limit
107 api.dispatch<any>(updatePublicFavorites(response.items.map((item: any) => item.uuid)));
109 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
110 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
114 rowsPerPage: dataExplorer.rowsPerPage
116 api.dispatch(couldNotFetchPublicFavorites());
122 const favoritesPanelDataExplorerIsNotSet = () =>
123 snackbarActions.OPEN_SNACKBAR({
124 message: 'Favorites panel is not ready.',
125 kind: SnackbarKind.ERROR
128 const couldNotFetchPublicFavorites = () =>
129 snackbarActions.OPEN_SNACKBAR({
130 message: 'Could not fetch public favorites contents.',
131 kind: SnackbarKind.ERROR