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 { SortDirection } from "components/data-table/data-column";
12 import { FilterBuilder } from "services/api/filter-builder";
13 import { updateFavorites } from "../favorites/favorites-actions";
14 import { favoritePanelActions } from "./favorite-panel-action";
15 import { Dispatch, MiddlewareAPI } from "redux";
16 import { OrderBuilder, OrderDirection } from "services/api/order-builder";
17 import { LinkResource } from "models/link";
18 import { GroupContentsResource, GroupContentsResourcePrefix } from "services/groups-service/groups-service";
19 import { resourcesActions } from "store/resources/resources-actions";
20 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
21 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
22 import { getDataExplorer } from "store/data-explorer/data-explorer-reducer";
23 import { loadMissingProcessesInformation } from "store/project-panel/project-panel-middleware-service";
24 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
25 import { getDataExplorerColumnFilters } from 'store/data-explorer/data-explorer-middleware-service';
26 import { serializeSimpleObjectTypeFilters } from '../resource-type-filters/resource-type-filters';
27 import { ResourceKind } from "models/resource";
28 import { LinkClass } from "models/link";
30 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
31 constructor(private services: ServiceRepository, id: string) {
35 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
36 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
38 api.dispatch(favoritesPanelDataExplorerIsNotSet());
40 const columns = dataExplorer.columns as DataColumns<string>;
41 const sortColumn = getSortColumn(dataExplorer);
42 const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
45 const linkOrder = new OrderBuilder<LinkResource>();
46 const contentOrder = new OrderBuilder<GroupContentsResource>();
48 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
49 const direction = sortColumn.sortDirection === SortDirection.ASC
51 : OrderDirection.DESC;
53 linkOrder.addOrder(direction, "name");
55 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
56 .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
57 .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
60 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
61 const responseLinks = await this.services.linkService.list({
62 filters: new FilterBuilder()
63 .addEqual("link_class", LinkClass.STAR)
64 .addEqual('tail_uuid', getUserUuid(api.getState()))
65 .addEqual('tail_kind', ResourceKind.USER)
67 }).then(results => results);
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.forEach((it: any) => {
92 response.itemsAvailable++;
93 response.items.push(it);
95 processItems.items.forEach((it: any) => {
96 response.itemsAvailable++;
97 response.items.push(it);
100 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
101 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
102 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
103 api.dispatch(favoritePanelActions.SET_ITEMS({
104 items: response.items.map((resource: any) => resource.uuid),
105 itemsAvailable: response.itemsAvailable,
106 page: Math.floor(response.offset / response.limit),
107 rowsPerPage: response.limit
109 api.dispatch<any>(updateFavorites(response.items.map((item: any) => item.uuid)));
111 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
112 api.dispatch(favoritePanelActions.SET_ITEMS({
116 rowsPerPage: dataExplorer.rowsPerPage
118 api.dispatch(couldNotFetchFavoritesContents());
124 const favoritesPanelDataExplorerIsNotSet = () =>
125 snackbarActions.OPEN_SNACKBAR({
126 message: 'Favorites panel is not ready.',
127 kind: SnackbarKind.ERROR
130 const couldNotFetchFavoritesContents = () =>
131 snackbarActions.OPEN_SNACKBAR({
132 message: 'Could not fetch favorites contents.',
133 kind: SnackbarKind.ERROR