1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { DataExplorerMiddlewareService } from "store/data-explorer/data-explorer-middleware-service";
6 import { FavoritePanelColumnNames } from "views/favorite-panel/favorite-panel";
7 import { RootState } from "../store";
8 import { getUserUuid } from "common/getuser";
9 import { DataColumns } from "components/data-table/data-table";
10 import { ServiceRepository } from "services/services";
11 import { FilterBuilder } from "services/api/filter-builder";
12 import { updateFavorites } from "../favorites/favorites-actions";
13 import { favoritePanelActions } from "./favorite-panel-action";
14 import { Dispatch, MiddlewareAPI } from "redux";
15 import { resourcesActions } from "store/resources/resources-actions";
16 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
17 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
18 import { getDataExplorer } from "store/data-explorer/data-explorer-reducer";
19 import { loadMissingProcessesInformation } from "store/project-panel/project-panel-middleware-service";
20 import { getDataExplorerColumnFilters } from 'store/data-explorer/data-explorer-middleware-service';
21 import { serializeSimpleObjectTypeFilters } from '../resource-type-filters/resource-type-filters';
22 import { ResourceKind } from "models/resource";
23 import { LinkClass } from "models/link";
24 import { GroupContentsResource } from "services/groups-service/groups-service";
26 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
27 constructor(private services: ServiceRepository, id: string) {
31 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
32 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
34 api.dispatch(favoritesPanelDataExplorerIsNotSet());
36 const columns = dataExplorer.columns as DataColumns<string, GroupContentsResource>;
37 const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
40 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
41 const responseLinks = await this.services.linkService.list({
42 filters: new FilterBuilder()
43 .addEqual("link_class", LinkClass.STAR)
44 .addEqual('tail_uuid', getUserUuid(api.getState()))
45 .addEqual('tail_kind', ResourceKind.USER)
47 }).then(results => results);
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);
80 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
81 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
82 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
83 api.dispatch(favoritePanelActions.SET_ITEMS({
84 items: response.items.map((resource: any) => resource.uuid),
85 itemsAvailable: response.itemsAvailable,
86 page: Math.floor(response.offset / response.limit),
87 rowsPerPage: response.limit
89 api.dispatch<any>(updateFavorites(response.items.map((item: any) => item.uuid)));
91 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
92 api.dispatch(favoritePanelActions.SET_ITEMS({
96 rowsPerPage: dataExplorer.rowsPerPage
98 api.dispatch(couldNotFetchFavoritesContents());
104 const favoritesPanelDataExplorerIsNotSet = () =>
105 snackbarActions.OPEN_SNACKBAR({
106 message: 'Favorites panel is not ready.',
107 kind: SnackbarKind.ERROR
110 const couldNotFetchFavoritesContents = () =>
111 snackbarActions.OPEN_SNACKBAR({
112 message: 'Could not fetch favorites contents.',
113 kind: SnackbarKind.ERROR