1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { DataExplorerMiddlewareService } from "../data-explorer/data-explorer-middleware-service";
6 import { FavoritePanelColumnNames, FavoritePanelFilter } 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";
24 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
25 constructor(private services: ServiceRepository, id: string) {
29 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
30 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
32 api.dispatch(favoritesPanelDataExplorerIsNotSet());
34 const columns = dataExplorer.columns as DataColumns<string, FavoritePanelFilter>;
35 const sortColumn = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
36 const typeFilters = this.getColumnFilters(columns, FavoritePanelColumnNames.TYPE);
38 const linkOrder = new OrderBuilder<LinkResource>();
39 const contentOrder = new OrderBuilder<GroupContentsResource>();
41 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
42 const direction = sortColumn.sortDirection === SortDirection.ASC
44 : OrderDirection.DESC;
46 linkOrder.addOrder(direction, "name");
48 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
49 .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
50 .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
53 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
54 const response = await this.services.favoriteService
55 .list(this.services.authService.getUuid()!, {
56 limit: dataExplorer.rowsPerPage,
57 offset: dataExplorer.page * dataExplorer.rowsPerPage,
58 linkOrder: linkOrder.getOrder(),
59 contentOrder: contentOrder.getOrder(),
60 filters: new FilterBuilder()
61 .addIsA("headUuid", typeFilters.map(filter => filter.type))
62 .addILike("name", dataExplorer.searchValue)
65 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
66 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
67 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
68 api.dispatch(favoritePanelActions.SET_ITEMS({
69 items: response.items.map(resource => resource.uuid),
70 itemsAvailable: response.itemsAvailable,
71 page: Math.floor(response.offset / response.limit),
72 rowsPerPage: response.limit
74 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
76 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
77 api.dispatch(favoritePanelActions.SET_ITEMS({
81 rowsPerPage: dataExplorer.rowsPerPage
83 api.dispatch(couldNotFetchFavoritesContents());
89 const favoritesPanelDataExplorerIsNotSet = () =>
90 snackbarActions.OPEN_SNACKBAR({
91 message: 'Favorites panel is not ready.'
94 const couldNotFetchFavoritesContents = () =>
95 snackbarActions.OPEN_SNACKBAR({
96 message: 'Could not fetch favorites contents.',
97 kind: SnackbarKind.ERROR