15088: Adds the ability to cancel account linking after logging in
[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 { 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 "~/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 { OrderBuilder, OrderDirection } from "~/services/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, SnackbarKind } from '~/store/snackbar/snackbar-actions';
20 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions.ts';
21 import { getDataExplorer } from "~/store/data-explorer/data-explorer-reducer";
22 import { loadMissingProcessesInformation } from "~/store/project-panel/project-panel-middleware-service";
23 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
24 import { getDataExplorerColumnFilters } from '~/store/data-explorer/data-explorer-middleware-service';
25 import { serializeSimpleObjectTypeFilters } from '../resource-type-filters/resource-type-filters';
26 import { ResourceKind } from "~/models/resource";
27
28 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
29     constructor(private services: ServiceRepository, id: string) {
30         super(id);
31     }
32
33     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
34         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
35         if (!dataExplorer) {
36             api.dispatch(favoritesPanelDataExplorerIsNotSet());
37         } else {
38             const columns = dataExplorer.columns as DataColumns<string>;
39             const sortColumn = getSortColumn(dataExplorer);
40             const typeFilters = serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
41
42
43             const linkOrder = new OrderBuilder<LinkResource>();
44             const contentOrder = new OrderBuilder<GroupContentsResource>();
45
46             if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
47                 const direction = sortColumn.sortDirection === SortDirection.ASC
48                     ? OrderDirection.ASC
49                     : OrderDirection.DESC;
50
51                 linkOrder.addOrder(direction, "name");
52                 contentOrder
53                     .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION)
54                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROCESS)
55                     .addOrder(direction, "name", GroupContentsResourcePrefix.PROJECT);
56             }
57             try {
58                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
59                 const resp1 = await this.services.linkService.list({
60                     filters: new FilterBuilder()
61                         .addEqual("linkClass", 'star')
62                         .addEqual('tailUuid', this.services.authService.getUuid()!)
63                         .addEqual('tailKind', ResourceKind.USER)
64                         .getFilters()
65                 }).then(results => results);
66                 const uuids = resp1.items.map(it => it.headUuid);
67                 const groupItems: any = await this.services.groupsService.list({
68                     filters: new FilterBuilder()
69                         .addIn("uuid", uuids)
70                         .addILike("name", dataExplorer.searchValue)
71                         .addIsA("uuid", typeFilters)
72                         .getFilters()
73                 });
74                 const collectionItems: any = await this.services.collectionService.list({
75                     filters: new FilterBuilder()
76                         .addIn("uuid", uuids)
77                         .addILike("name", dataExplorer.searchValue)
78                         .addIsA("uuid", typeFilters)
79                         .getFilters()
80                 });
81                 const processItems: any = await this.services.containerRequestService.list({
82                     filters: new FilterBuilder()
83                         .addIn("uuid", uuids)
84                         .addILike("name", dataExplorer.searchValue)
85                         .addIsA("uuid", typeFilters)
86                         .getFilters()
87                 });
88                 const response = groupItems;
89                 collectionItems.items.map((it: any) => {
90                     response.itemsAvailable++;
91                     response.items.push(it);
92                 });
93                 processItems.items.map((it: any) => {
94                     response.itemsAvailable++;
95                     response.items.push(it);
96                 });
97
98                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
99                 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
100                 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
101                 api.dispatch(favoritePanelActions.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>(updateFavorites(response.items.map((item: any) => item.uuid)));
108             } catch (e) {
109                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
110                 api.dispatch(favoritePanelActions.SET_ITEMS({
111                     items: [],
112                     itemsAvailable: 0,
113                     page: 0,
114                     rowsPerPage: dataExplorer.rowsPerPage
115                 }));
116                 api.dispatch(couldNotFetchFavoritesContents());
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 couldNotFetchFavoritesContents = () =>
129     snackbarActions.OPEN_SNACKBAR({
130         message: 'Could not fetch favorites contents.',
131         kind: SnackbarKind.ERROR
132     });