21128: cleanup, renamed some types Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[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 "store/data-explorer/data-explorer-middleware-service";
6 import { FavoritePanelColumnNames } from "views/favorite-panel/favorite-panel";
7 import { RootState } from "../store";
8 import { getUserUuid } from "common/getuser";
9 import { DataColumns } from "components/data-table/data-table";
10 import { ServiceRepository } from "services/services";
11 import { FilterBuilder } from "services/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 { resourcesActions } from "store/resources/resources-actions";
16 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
17 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
18 import { getDataExplorer } from "store/data-explorer/data-explorer-reducer";
19 import { loadMissingProcessesInformation } from "store/project-panel/project-panel-middleware-service";
20 import { getDataExplorerColumnFilters } from 'store/data-explorer/data-explorer-middleware-service';
21 import { serializeSimpleObjectTypeFilters } from '../resource-type-filters/resource-type-filters';
22 import { ResourceKind } from "models/resource";
23 import { LinkClass } from "models/link";
24 import { GroupContentsResource } from "services/groups-service/groups-service";
25
26 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
27     constructor(private services: ServiceRepository, id: string) {
28         super(id);
29     }
30
31     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
32         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
33         if (!dataExplorer) {
34             api.dispatch(favoritesPanelDataExplorerIsNotSet());
35         } else {
36             const columns = dataExplorer.columns as DataColumns<string, GroupContentsResource>;
37             const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
38
39             try {
40                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
41                 const responseLinks = await this.services.linkService.list({
42                     filters: new FilterBuilder()
43                         .addEqual("link_class", LinkClass.STAR)
44                         .addEqual('tail_uuid', getUserUuid(api.getState()))
45                         .addEqual('tail_kind', ResourceKind.USER)
46                         .getFilters()
47                 }).then(results => results);
48                 const uuids = responseLinks.items.map(it => it.headUuid);
49                 const groupItems: any = await this.services.groupsService.list({
50                     filters: new FilterBuilder()
51                         .addIn("uuid", uuids)
52                         .addILike("name", dataExplorer.searchValue)
53                         .addIsA("uuid", typeFilters)
54                         .getFilters()
55                 });
56                 const collectionItems: any = await this.services.collectionService.list({
57                     filters: new FilterBuilder()
58                         .addIn("uuid", uuids)
59                         .addILike("name", dataExplorer.searchValue)
60                         .addIsA("uuid", typeFilters)
61                         .getFilters()
62                 });
63                 const processItems: any = await this.services.containerRequestService.list({
64                     filters: new FilterBuilder()
65                         .addIn("uuid", uuids)
66                         .addILike("name", dataExplorer.searchValue)
67                         .addIsA("uuid", typeFilters)
68                         .getFilters()
69                 });
70                 const response = groupItems;
71                 collectionItems.items.forEach((it: any) => {
72                     response.itemsAvailable++;
73                     response.items.push(it);
74                 });
75                 processItems.items.forEach((it: any) => {
76                     response.itemsAvailable++;
77                     response.items.push(it);
78                 });
79
80                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
81                 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
82                 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
83                 api.dispatch(favoritePanelActions.SET_ITEMS({
84                     items: response.items.map((resource: any) => resource.uuid),
85                     itemsAvailable: response.itemsAvailable,
86                     page: Math.floor(response.offset / response.limit),
87                     rowsPerPage: response.limit
88                 }));
89                 api.dispatch<any>(updateFavorites(response.items.map((item: any) => item.uuid)));
90             } catch (e) {
91                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
92                 api.dispatch(favoritePanelActions.SET_ITEMS({
93                     items: [],
94                     itemsAvailable: 0,
95                     page: 0,
96                     rowsPerPage: dataExplorer.rowsPerPage
97                 }));
98                 api.dispatch(couldNotFetchFavoritesContents());
99             }
100         }
101     }
102 }
103
104 const favoritesPanelDataExplorerIsNotSet = () =>
105     snackbarActions.OPEN_SNACKBAR({
106         message: 'Favorites panel is not ready.',
107         kind: SnackbarKind.ERROR
108     });
109
110 const couldNotFetchFavoritesContents = () =>
111     snackbarActions.OPEN_SNACKBAR({
112         message: 'Could not fetch favorites contents.',
113         kind: SnackbarKind.ERROR
114     });