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 { 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";
20 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
21 constructor(private services: ServiceRepository, id: string) {
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);
31 const linkOrder = new OrderBuilder<LinkResource>();
32 const contentOrder = new OrderBuilder<GroupContentsResource>();
34 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
35 const direction = sortColumn.sortDirection === SortDirection.ASC
37 : OrderDirection.DESC;
39 linkOrder.addOrder(direction, "name");
41 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
42 .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
43 .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
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)
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
64 api.dispatch<any>(checkPresenceInFavorites(response.items.map(item => item.uuid)));
67 api.dispatch(favoritePanelActions.SET_ITEMS({
71 rowsPerPage: dataExplorer.rowsPerPage