refs #14370-display-correct-description
[arvados-workbench2.git] / src / store / favorite-panel / favorite-panel-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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
27 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
28     constructor(private services: ServiceRepository, id: string) {
29         super(id);
30     }
31
32     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
33         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
34         if (!dataExplorer) {
35             api.dispatch(favoritesPanelDataExplorerIsNotSet());
36         } else {
37             const columns = dataExplorer.columns as DataColumns<string>;
38             const sortColumn = getSortColumn(dataExplorer);
39             const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
40
41
42             const linkOrder = new OrderBuilder<LinkResource>();
43             const contentOrder = new OrderBuilder<GroupContentsResource>();
44
45             if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
46                 const direction = sortColumn.sortDirection === SortDirection.ASC
47                     ? OrderDirection.ASC
48                     : OrderDirection.DESC;
49
50                 linkOrder.addOrder(direction, "name");
51                 contentOrder
52                     .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
53                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
54                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
55             }
56             try {
57                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
58                 const response = await this.services.favoriteService
59                     .list(this.services.authService.getUuid()!, {
60                         limit: dataExplorer.rowsPerPage,
61                         offset: dataExplorer.page * dataExplorer.rowsPerPage,
62                         linkOrder: linkOrder.getOrder(),
63                         contentOrder: contentOrder.getOrder(),
64                         filters: new FilterBuilder()
65                             .addILike("name", dataExplorer.searchValue)
66                             .addIsA("headUuid", typeFilters)
67                             .getFilters(),
68
69                     });
70                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
71                 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
72                 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
73                 api.dispatch(favoritePanelActions.SET_ITEMS({
74                     items: response.items.map(resource => resource.uuid),
75                     itemsAvailable: response.itemsAvailable,
76                     page: Math.floor(response.offset / response.limit),
77                     rowsPerPage: response.limit
78                 }));
79                 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
80             } catch (e) {
81                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
82                 api.dispatch(favoritePanelActions.SET_ITEMS({
83                     items: [],
84                     itemsAvailable: 0,
85                     page: 0,
86                     rowsPerPage: dataExplorer.rowsPerPage
87                 }));
88                 api.dispatch(couldNotFetchFavoritesContents());
89             }
90         }
91     }
92 }
93
94 const favoritesPanelDataExplorerIsNotSet = () =>
95     snackbarActions.OPEN_SNACKBAR({
96         message: 'Favorites panel is not ready.',
97         kind: SnackbarKind.ERROR
98     });
99
100 const couldNotFetchFavoritesContents = () =>
101     snackbarActions.OPEN_SNACKBAR({
102         message: 'Could not fetch favorites contents.',
103         kind: SnackbarKind.ERROR
104     });