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 { DataColumns } from "~/components/data-table/data-table";
9 import { ServiceRepository } from "~/services/services";
10 import { SortDirection } from "~/components/data-table/data-column";
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 { OrderBuilder, OrderDirection } from "~/services/api/order-builder";
16 import { LinkResource } from "~/models/link";
17 import { GroupContentsResource, GroupContentsResourcePrefix } from "~/services/groups-service/groups-service";
18 import { resourcesActions } from "~/store/resources/resources-actions";
19 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
20 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions.ts';
21 import { getDataExplorer } from "~/store/data-explorer/data-explorer-reducer";
22 import { loadMissingProcessesInformation } from "~/store/project-panel/project-panel-middleware-service";
23 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
24 import { getDataExplorerColumnFilters } from '~/store/data-explorer/data-explorer-middleware-service';
25 import { serializeSimpleObjectTypeFilters } from '../resource-type-filters/resource-type-filters';
26 import { ResourceKind } from "~/models/resource";
28 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
29 constructor(private services: ServiceRepository, id: string) {
33 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
34 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
36 api.dispatch(favoritesPanelDataExplorerIsNotSet());
38 const columns = dataExplorer.columns as DataColumns<string>;
39 const sortColumn = getSortColumn(dataExplorer);
40 const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
43 const linkOrder = new OrderBuilder<LinkResource>();
44 const contentOrder = new OrderBuilder<GroupContentsResource>();
46 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
47 const direction = sortColumn.sortDirection === SortDirection.ASC
49 : OrderDirection.DESC;
51 linkOrder.addOrder(direction, "name");
53 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
54 .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
55 .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
58 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
59 const resp1 = await this.services.linkService.list({
60 filters: new FilterBuilder()
61 .addEqual("linkClass", 'star')
62 .addEqual('tailUuid', this.services.authService.getUuid()!)
63 .addEqual('tailKind', ResourceKind.USER)
65 }).then(results => results);
66 const uuids = resp1.items.map(it => it.headUuid);
67 const groupItems: any = await this.services.groupsService.list({
68 filters: new FilterBuilder()
70 .addILike("name", dataExplorer.searchValue)
71 .addIsA("uuid", typeFilters)
74 const collectionItems: any = await this.services.collectionService.list({
75 filters: new FilterBuilder()
77 .addILike("name", dataExplorer.searchValue)
78 .addIsA("uuid", typeFilters)
81 const processItems: any = await this.services.containerRequestService.list({
82 filters: new FilterBuilder()
84 .addILike("name", dataExplorer.searchValue)
85 .addIsA("uuid", typeFilters)
88 const response = groupItems;
89 collectionItems.items.map((it: any) => {
90 response.itemsAvailable++;
91 response.items.push(it);
93 processItems.items.map((it: any) => {
94 response.itemsAvailable++;
95 response.items.push(it);
98 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
99 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
100 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
101 api.dispatch(favoritePanelActions.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>(updateFavorites(response.items.map((item: any) => item.uuid)));
109 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
110 api.dispatch(favoritePanelActions.SET_ITEMS({
114 rowsPerPage: dataExplorer.rowsPerPage
116 api.dispatch(couldNotFetchFavoritesContents());
122 const favoritesPanelDataExplorerIsNotSet = () =>
123 snackbarActions.OPEN_SNACKBAR({
124 message: 'Favorites panel is not ready.',
125 kind: SnackbarKind.ERROR
128 const couldNotFetchFavoritesContents = () =>
129 snackbarActions.OPEN_SNACKBAR({
130 message: 'Could not fetch favorites contents.',
131 kind: SnackbarKind.ERROR