remove uuid from group service
[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 "../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";
23
24 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
25     constructor(private services: ServiceRepository, id: string) {
26         super(id);
27     }
28
29     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
30         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
31         if (!dataExplorer) {
32             api.dispatch(favoritesPanelDataExplorerIsNotSet());
33         } else {
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);
37
38             const linkOrder = new OrderBuilder<LinkResource>();
39             const contentOrder = new OrderBuilder<GroupContentsResource>();
40
41             if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
42                 const direction = sortColumn.sortDirection === SortDirection.ASC
43                     ? OrderDirection.ASC
44                     : OrderDirection.DESC;
45
46                 linkOrder.addOrder(direction, "name");
47                 contentOrder
48                     .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
49                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
50                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
51             }
52             try {
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)
63                             .getFilters()
64                     });
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
73                 }));
74                 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
75             } catch (e) {
76                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
77                 api.dispatch(favoritePanelActions.SET_ITEMS({
78                     items: [],
79                     itemsAvailable: 0,
80                     page: 0,
81                     rowsPerPage: dataExplorer.rowsPerPage
82                 }));
83                 api.dispatch(couldNotFetchFavoritesContents());
84             }
85         }
86     }
87 }
88
89 const favoritesPanelDataExplorerIsNotSet = () =>
90     snackbarActions.OPEN_SNACKBAR({
91         message: 'Favorites panel is not ready.'
92     });
93
94 const couldNotFetchFavoritesContents = () =>
95     snackbarActions.OPEN_SNACKBAR({
96         message: 'Could not fetch favorites contents.',
97         kind: SnackbarKind.ERROR
98     });