17119: add basic filter group support.
[arvados.git] / src / store / public-favorites-panel / public-favorites-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ServiceRepository } from '~/services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService, getDataExplorerColumnFilters } from '~/store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from '~/store/store';
9 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
10 import { getDataExplorer } from '~/store/data-explorer/data-explorer-reducer';
11 import { resourcesActions } from '~/store/resources/resources-actions';
12 import { FilterBuilder } from '~/services/api/filter-builder';
13 import { SortDirection } from '~/components/data-table/data-column';
14 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
15 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
16 import { FavoritePanelColumnNames } from '~/views/favorite-panel/favorite-panel';
17 import { publicFavoritePanelActions } from '~/store/public-favorites-panel/public-favorites-action';
18 import { DataColumns } from '~/components/data-table/data-table';
19 import { serializeSimpleObjectTypeFilters } from '../resource-type-filters/resource-type-filters';
20 import { LinkResource, LinkClass } from '~/models/link';
21 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
22 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
23 import { updatePublicFavorites } from '~/store/public-favorites/public-favorites-actions';
24
25 export class PublicFavoritesMiddlewareService extends DataExplorerMiddlewareService {
26     constructor(private services: ServiceRepository, id: string) {
27         super(id);
28     }
29
30     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
31         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
32         if (!dataExplorer) {
33             api.dispatch(favoritesPanelDataExplorerIsNotSet());
34         } else {
35             const columns = dataExplorer.columns as DataColumns<string>;
36             const sortColumn = getSortColumn(dataExplorer);
37             const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
38
39
40             const linkOrder = new OrderBuilder<LinkResource>();
41             const contentOrder = new OrderBuilder<GroupContentsResource>();
42
43             if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
44                 const direction = sortColumn.sortDirection === SortDirection.ASC
45                     ? OrderDirection.ASC
46                     : OrderDirection.DESC;
47
48                 linkOrder.addOrder(direction, "name");
49                 contentOrder
50                     .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
51                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
52                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
53             }
54             try {
55                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
56                 const uuidPrefix = api.getState().auth.config.uuidPrefix;
57                 const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
58                 const responseLinks = await this.services.linkService.list({
59                     limit: dataExplorer.rowsPerPage,
60                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
61                     filters: new FilterBuilder()
62                         .addEqual('link_class', LinkClass.STAR)
63                         .addEqual('owner_uuid', publicProjectUuid)
64                         .addIsA("head_uuid", typeFilters)
65                         .getFilters()
66                 });
67                 const uuids = responseLinks.items.map(it => it.headUuid);
68                 const groupItems: any = await this.services.groupsService.list({
69                     filters: new FilterBuilder()
70                         .addIn("uuid", uuids)
71                         .addILike("name", dataExplorer.searchValue)
72                         .addIsA("uuid", typeFilters)
73                         .getFilters()
74                 });
75                 const collectionItems: any = await this.services.collectionService.list({
76                     filters: new FilterBuilder()
77                         .addIn("uuid", uuids)
78                         .addILike("name", dataExplorer.searchValue)
79                         .addIsA("uuid", typeFilters)
80                         .getFilters()
81                 });
82                 const processItems: any = await this.services.containerRequestService.list({
83                     filters: new FilterBuilder()
84                         .addIn("uuid", uuids)
85                         .addILike("name", dataExplorer.searchValue)
86                         .addIsA("uuid", typeFilters)
87                         .getFilters()
88                 });
89                 const response = groupItems;
90                 collectionItems.items.map((it: any) => {
91                     response.itemsAvailable++;
92                     response.items.push(it);
93                 });
94                 processItems.items.map((it: any) => {
95                     response.itemsAvailable++;
96                     response.items.push(it);
97                 });
98                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
99                 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
100                 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
101                     items: response.items.map((resource: any) => resource.uuid),
102                     itemsAvailable: response.itemsAvailable,
103                     page: Math.floor(response.offset / response.limit),
104                     rowsPerPage: response.limit
105                 }));
106                 api.dispatch<any>(updatePublicFavorites(response.items.map((item: any) => item.uuid)));
107             } catch (e) {
108                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
109                 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
110                     items: [],
111                     itemsAvailable: 0,
112                     page: 0,
113                     rowsPerPage: dataExplorer.rowsPerPage
114                 }));
115                 api.dispatch(couldNotFetchPublicFavorites());
116             }
117         }
118     }
119 }
120
121 const favoritesPanelDataExplorerIsNotSet = () =>
122     snackbarActions.OPEN_SNACKBAR({
123         message: 'Favorites panel is not ready.',
124         kind: SnackbarKind.ERROR
125     });
126
127 const couldNotFetchPublicFavorites = () =>
128     snackbarActions.OPEN_SNACKBAR({
129         message: 'Could not fetch public favorites contents.',
130         kind: SnackbarKind.ERROR
131     });