cr changes
[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 { ServiceRepository } from "~/services/services";
10 import { SortDirection } from "~/components/data-table/data-column";
11 import { FilterBuilder } from "~/common/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 "~/common/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 } from '~/store/snackbar/snackbar-actions';
20 import { getDataExplorer } from "~/store/data-explorer/data-explorer-reducer";
21
22 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
23     constructor(private services: ServiceRepository, id: string) {
24         super(id);
25     }
26
27     requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
28         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
29         if (!dataExplorer) {
30             api.dispatch(favoritesPanelDataExplorerIsNotSet());
31          } else {
32
33             const columns = dataExplorer.columns as DataColumns<string, FavoritePanelFilter>;
34             const sortColumn = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
35             const typeFilters = this.getColumnFilters(columns, FavoritePanelColumnNames.TYPE);
36
37             const linkOrder = new OrderBuilder<LinkResource>();
38             const contentOrder = new OrderBuilder<GroupContentsResource>();
39
40             if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
41                 const direction = sortColumn.sortDirection === SortDirection.ASC
42                     ? OrderDirection.ASC
43                     : OrderDirection.DESC;
44
45                 linkOrder.addOrder(direction, "name");
46                 contentOrder
47                     .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
48                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
49                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
50             }
51
52             this.services.favoriteService
53                 .list(this.services.authService.getUuid()!, {
54                     limit: dataExplorer.rowsPerPage,
55                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
56                     linkOrder: linkOrder.getOrder(),
57                     contentOrder: contentOrder.getOrder(),
58                     filters: new FilterBuilder()
59                         .addIsA("headUuid", typeFilters.map(filter => filter.type))
60                         .addILike("name", dataExplorer.searchValue)
61                         .getFilters()
62                 })
63                 .then(response => {
64                     api.dispatch(resourcesActions.SET_RESOURCES(response.items));
65                     api.dispatch(favoritePanelActions.SET_ITEMS({
66                         items: response.items.map(resource => resource.uuid),
67                         itemsAvailable: response.itemsAvailable,
68                         page: Math.floor(response.offset / response.limit),
69                         rowsPerPage: response.limit
70                     }));
71                     api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
72                 })
73                 .catch(() => {
74                     api.dispatch(favoritePanelActions.SET_ITEMS({
75                         items: [],
76                         itemsAvailable: 0,
77                         page: 0,
78                         rowsPerPage: dataExplorer.rowsPerPage
79                     }));
80                 });
81         }
82     }
83 }
84
85 const favoritesPanelDataExplorerIsNotSet = () =>
86     snackbarActions.OPEN_SNACKBAR({
87         message: 'Favorites panel is not ready.'
88     });