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.ts';
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";
29 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
30 constructor(private services: ServiceRepository, id: string) {
34 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
35 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
37 api.dispatch(favoritesPanelDataExplorerIsNotSet());
39 const columns = dataExplorer.columns as DataColumns<string>;
40 const sortColumn = getSortColumn(dataExplorer);
41 const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
44 const linkOrder = new OrderBuilder<LinkResource>();
45 const contentOrder = new OrderBuilder<GroupContentsResource>();
47 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
48 const direction = sortColumn.sortDirection === SortDirection.ASC
50 : OrderDirection.DESC;
52 linkOrder.addOrder(direction, "name");
54 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
55 .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
56 .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
59 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
60 const responseLinks = await this.services.linkService.list({
61 filters: new FilterBuilder()
62 .addEqual("link_class", 'star')
63 .addEqual('tail_uuid', getUserUuid(api.getState()))
64 .addEqual('tail_kind', ResourceKind.USER)
66 }).then(results => results);
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);
99 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
100 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
101 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
102 api.dispatch(favoritePanelActions.SET_ITEMS({
103 items: response.items.map((resource: any) => resource.uuid),
104 itemsAvailable: response.itemsAvailable,
105 page: Math.floor(response.offset / response.limit),
106 rowsPerPage: response.limit
108 api.dispatch<any>(updateFavorites(response.items.map((item: any) => item.uuid)));
110 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
111 api.dispatch(favoritePanelActions.SET_ITEMS({
115 rowsPerPage: dataExplorer.rowsPerPage
117 api.dispatch(couldNotFetchFavoritesContents());
123 const favoritesPanelDataExplorerIsNotSet = () =>
124 snackbarActions.OPEN_SNACKBAR({
125 message: 'Favorites panel is not ready.',
126 kind: SnackbarKind.ERROR
129 const couldNotFetchFavoritesContents = () =>
130 snackbarActions.OPEN_SNACKBAR({
131 message: 'Could not fetch favorites contents.',
132 kind: SnackbarKind.ERROR