15814: Catch error that otherwise goes unhandled
[arvados.git] / services / workbench2 / src / store / trash-panel / trash-panel-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import {
6     DataExplorerMiddlewareService, dataExplorerToListParams,
7     listResultsToDataExplorerItemsMeta
8 } from "../data-explorer/data-explorer-middleware-service";
9 import { RootState } from "../store";
10 import { getUserUuid } from "common/getuser";
11 import { DataColumns } from "components/data-table/data-table";
12 import { ServiceRepository } from "services/services";
13 import { SortDirection } from "components/data-table/data-column";
14 import { FilterBuilder } from "services/api/filter-builder";
15 import { trashPanelActions } from "./trash-panel-action";
16 import { Dispatch, MiddlewareAPI } from "redux";
17 import { OrderBuilder, OrderDirection } from "services/api/order-builder";
18 import { GroupContentsResource, GroupContentsResourcePrefix } from "services/groups-service/groups-service";
19 import { TrashPanelColumnNames } from "views/trash-panel/trash-panel";
20 import { updateFavorites } from "store/favorites/favorites-actions";
21 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
22 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
23 import { updateResources } from "store/resources/resources-actions";
24 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
25 import { DataExplorer, getSortColumn } from "store/data-explorer/data-explorer-reducer";
26 import { serializeResourceTypeFilters } from 'store//resource-type-filters/resource-type-filters';
27 import { getDataExplorerColumnFilters } from 'store/data-explorer/data-explorer-middleware-service';
28 import { joinFilters } from 'services/api/filter-builder';
29 import { CollectionResource } from "models/collection";
30 import { ContextMenuActionNames } from "views-components/context-menu/context-menu-action-set";
31 import { removeDisabledButton } from "store/multiselect/multiselect-actions";
32 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
33     constructor(private services: ServiceRepository, id: string) {
34         super(id);
35     }
36
37     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
38         const dataExplorer = api.getState().dataExplorer[this.getId()];
39         const columns = dataExplorer.columns as DataColumns<string, CollectionResource>;
40
41         const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, TrashPanelColumnNames.TYPE));
42
43         const otherFilters = new FilterBuilder()
44             .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
45             // .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROCESS)
46             .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
47             .addEqual("is_trashed", true)
48             .getFilters();
49
50         const filters = joinFilters(
51             typeFilters,
52             otherFilters,
53         );
54
55         const userUuid = getUserUuid(api.getState());
56         if (!userUuid) { return; }
57         try {
58             api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
59             const listResults = await this.services.groupsService
60                 .contents('', {
61                     ...dataExplorerToListParams(dataExplorer),
62                     order: getOrder(dataExplorer),
63                     filters,
64                     recursive: true,
65                     includeTrash: true
66                 });
67             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
68
69             const items = listResults.items.map(it => it.uuid);
70
71             api.dispatch(trashPanelActions.SET_ITEMS({
72                 ...listResultsToDataExplorerItemsMeta(listResults),
73                 items
74             }));
75             api.dispatch<any>(updateFavorites(items));
76             api.dispatch<any>(updatePublicFavorites(items));
77             api.dispatch(updateResources(listResults.items));
78         } catch (e) {
79             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
80             api.dispatch(trashPanelActions.SET_ITEMS({
81                 items: [],
82                 itemsAvailable: 0,
83                 page: 0,
84                 rowsPerPage: dataExplorer.rowsPerPage
85             }));
86             api.dispatch(couldNotFetchTrashContents());
87         }
88         api.dispatch<any>(removeDisabledButton(ContextMenuActionNames.MOVE_TO_TRASH))
89     }
90 }
91
92 const getOrder = (dataExplorer: DataExplorer) => {
93     const sortColumn = getSortColumn<GroupContentsResource>(dataExplorer);
94     const order = new OrderBuilder<GroupContentsResource>();
95     if (sortColumn && sortColumn.sort) {
96         const sortDirection = sortColumn.sort.direction === SortDirection.ASC
97             ? OrderDirection.ASC
98             : OrderDirection.DESC;
99
100         // Use createdAt as a secondary sort column so we break ties consistently.
101         return order
102             .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.COLLECTION)
103             .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.PROJECT)
104             .addOrder(OrderDirection.DESC, "createdAt", GroupContentsResourcePrefix.PROCESS)
105             .getOrder();
106     } else {
107         return order.getOrder();
108     }
109 };
110
111 const couldNotFetchTrashContents = () =>
112     snackbarActions.OPEN_SNACKBAR({
113         message: 'Could not fetch trash contents.',
114         kind: SnackbarKind.ERROR
115     });