cr changes + tests fix
[arvados.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 { FavoritePanelItem, resourceToDataItem } from "~/views/favorite-panel/favorite-panel-item";
10 import { ServiceRepository } from "~/services/services";
11 import { SortDirection } from "~/components/data-table/data-column";
12 import { FilterBuilder } from "~/common/api/filter-builder";
13 import { checkPresenceInFavorites } from "../favorites/favorites-actions";
14 import { favoritePanelActions } from "./favorite-panel-action";
15 import { Dispatch, MiddlewareAPI } from "redux";
16 import { OrderBuilder, OrderDirection } from "~/common/api/order-builder";
17 import { LinkResource } from "~/models/link";
18 import { GroupContentsResource, GroupContentsResourcePrefix } from "~/services/groups-service/groups-service";
19
20 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
21     constructor(private services: ServiceRepository, id: string) {
22         super(id);
23     }
24
25     requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
26         const dataExplorer = api.getState().dataExplorer[this.getId()];
27         const columns = dataExplorer.columns as DataColumns<FavoritePanelItem, FavoritePanelFilter>;
28         const sortColumn = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
29         const typeFilters = this.getColumnFilters(columns, FavoritePanelColumnNames.TYPE);
30
31         const linkOrder = new OrderBuilder<LinkResource>();
32         const contentOrder = new OrderBuilder<GroupContentsResource>();
33
34         if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
35             const direction = sortColumn.sortDirection === SortDirection.ASC
36                 ? OrderDirection.ASC
37                 : OrderDirection.DESC;
38
39             linkOrder.addOrder(direction, "name");
40             contentOrder
41                 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
42                 .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
43                 .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
44         }
45
46         this.services.favoriteService
47             .list(this.services.authService.getUuid()!, {
48                 limit: dataExplorer.rowsPerPage,
49                 offset: dataExplorer.page * dataExplorer.rowsPerPage,
50                 linkOrder: linkOrder.getOrder(),
51                 contentOrder: contentOrder.getOrder(),
52                 filters: new FilterBuilder()
53                     .addIsA("headUuid", typeFilters.map(filter => filter.type))
54                     .addILike("name", dataExplorer.searchValue)
55                     .getFilters()
56             })
57             .then(response => {
58                 api.dispatch(favoritePanelActions.SET_ITEMS({
59                     items: response.items.map(resourceToDataItem),
60                     itemsAvailable: response.itemsAvailable,
61                     page: Math.floor(response.offset / response.limit),
62                     rowsPerPage: response.limit
63                 }));
64                 api.dispatch<any>(checkPresenceInFavorites(response.items.map(item => item.uuid)));
65             })
66             .catch(() => {
67                 api.dispatch(favoritePanelActions.SET_ITEMS({
68                     items: [],
69                     itemsAvailable: 0,
70                     page: 0,
71                     rowsPerPage: dataExplorer.rowsPerPage
72                 }));
73             });
74     }
75 }