Merge branch '15256-removing-files-during-upload'
[arvados-workbench2.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 uuid = `${uuidPrefix}-j7d0g-fffffffffffffff`;
58                 const responseLinks = await this.services.linkService.list({
59                     limit: dataExplorer.rowsPerPage,
60                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
61                     filters: new FilterBuilder()
62                         .addEqual('linkClass', LinkClass.STAR)
63                         .addILike("name", dataExplorer.searchValue)
64                         .addEqual('ownerUuid', uuid)
65                         .addIsA("headUuid", typeFilters)
66                         .getFilters()
67                 });
68                 const uuids = responseLinks.items.map(it => it.headUuid);
69                 const groupItems: any = await this.services.groupsService.list({
70                     filters: new FilterBuilder()
71                         .addIn("uuid", uuids)
72                         .addILike("name", dataExplorer.searchValue)
73                         .addIsA("uuid", typeFilters)
74                         .getFilters()
75                 });
76                 const collectionItems: any = await this.services.collectionService.list({
77                     filters: new FilterBuilder()
78                         .addIn("uuid", uuids)
79                         .addILike("name", dataExplorer.searchValue)
80                         .addIsA("uuid", typeFilters)
81                         .getFilters()
82                 });
83                 const processItems: any = await this.services.containerRequestService.list({
84                     filters: new FilterBuilder()
85                         .addIn("uuid", uuids)
86                         .addILike("name", dataExplorer.searchValue)
87                         .addIsA("uuid", typeFilters)
88                         .getFilters()
89                 });
90                 const response = groupItems;
91                 collectionItems.items.map((it: any) => {
92                     response.itemsAvailable++;
93                     response.items.push(it);
94                 });
95                 processItems.items.map((it: any) => {
96                     response.itemsAvailable++;
97                     response.items.push(it);
98                 });
99                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
100                 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
101                 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
102                     items: response.items.map((resource: any) => resource.uuid),
103                     itemsAvailable: response.itemsAvailable,
104                     page: Math.floor(response.offset / response.limit),
105                     rowsPerPage: response.limit
106                 }));
107                 api.dispatch<any>(updatePublicFavorites(response.items.map((item: any) => item.uuid)));
108             } catch (e) {
109                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
110                 api.dispatch(publicFavoritePanelActions.SET_ITEMS({
111                     items: [],
112                     itemsAvailable: 0,
113                     page: 0,
114                     rowsPerPage: dataExplorer.rowsPerPage
115                 }));
116                 api.dispatch(couldNotFetchPublicFavorites());
117             }
118         }
119     }
120 }
121
122 const favoritesPanelDataExplorerIsNotSet = () =>
123     snackbarActions.OPEN_SNACKBAR({
124         message: 'Favorites panel is not ready.',
125         kind: SnackbarKind.ERROR
126     });
127
128 const couldNotFetchPublicFavorites = () =>
129     snackbarActions.OPEN_SNACKBAR({
130         message: 'Could not fetch public favorites contents.',
131         kind: SnackbarKind.ERROR
132     });