20846: Merge branch '19213-ubuntu2204-support' into 20846-ubuntu2204
[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 { ProjectPanelColumnNames } from "views/project-panel/project-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
31 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
32     constructor(private services: ServiceRepository, id: string) {
33         super(id);
34     }
35
36     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
37         const dataExplorer = api.getState().dataExplorer[this.getId()];
38         const columns = dataExplorer.columns as DataColumns<string, CollectionResource>;
39
40         const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
41
42         const otherFilters = new FilterBuilder()
43             .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
44             // .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROCESS)
45             .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
46             .addEqual("is_trashed", true)
47             .getFilters();
48
49         const filters = joinFilters(
50             typeFilters,
51             otherFilters,
52         );
53
54         const userUuid = getUserUuid(api.getState());
55         if (!userUuid) { return; }
56         try {
57             api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
58             const listResults = await this.services.groupsService
59                 .contents('', {
60                     ...dataExplorerToListParams(dataExplorer),
61                     order: getOrder(dataExplorer),
62                     filters,
63                     recursive: true,
64                     includeTrash: true
65                 });
66             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
67
68             const items = listResults.items.map(it => it.uuid);
69
70             api.dispatch(trashPanelActions.SET_ITEMS({
71                 ...listResultsToDataExplorerItemsMeta(listResults),
72                 items
73             }));
74             api.dispatch<any>(updateFavorites(items));
75             api.dispatch<any>(updatePublicFavorites(items));
76             api.dispatch(updateResources(listResults.items));
77         } catch (e) {
78             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
79             api.dispatch(trashPanelActions.SET_ITEMS({
80                 items: [],
81                 itemsAvailable: 0,
82                 page: 0,
83                 rowsPerPage: dataExplorer.rowsPerPage
84             }));
85             api.dispatch(couldNotFetchTrashContents());
86         }
87     }
88 }
89
90 const getOrder = (dataExplorer: DataExplorer) => {
91     const sortColumn = getSortColumn<GroupContentsResource>(dataExplorer);
92     const order = new OrderBuilder<GroupContentsResource>();
93     if (sortColumn && sortColumn.sort) {
94         const sortDirection = sortColumn.sort.direction === SortDirection.ASC
95             ? OrderDirection.ASC
96             : OrderDirection.DESC;
97
98         // Use createdAt as a secondary sort column so we break ties consistently.
99         return order
100             .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.COLLECTION)
101             .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.PROJECT)
102             .addOrder(OrderDirection.DESC, "createdAt", GroupContentsResourcePrefix.PROCESS)
103             .getOrder();
104     } else {
105         return order.getOrder();
106     }
107 };
108
109 const couldNotFetchTrashContents = () =>
110     snackbarActions.OPEN_SNACKBAR({
111         message: 'Could not fetch trash contents.',
112         kind: SnackbarKind.ERROR
113     });