16068: Merge branch 'main' of git.arvados.org:arvados-workbench2 into 16068
[arvados-workbench2.git] / 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 { GroupContentsResourcePrefix } from "services/groups-service/groups-service";
19 import { ProjectResource } from "models/project";
20 import { ProjectPanelColumnNames } from "views/project-panel/project-panel";
21 import { updateFavorites } from "store/favorites/favorites-actions";
22 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
23 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
24 import { updateResources } from "store/resources/resources-actions";
25 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
26 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
27 import { serializeResourceTypeFilters } from 'store//resource-type-filters/resource-type-filters';
28 import { getDataExplorerColumnFilters } from 'store/data-explorer/data-explorer-middleware-service';
29 import { joinFilters } from 'services/api/filter-builder';
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>;
39         const sortColumn = getSortColumn(dataExplorer);
40
41         const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.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 order = new OrderBuilder<ProjectResource>();
56
57         if (sortColumn) {
58             const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
59                 ? OrderDirection.ASC
60                 : OrderDirection.DESC;
61
62             const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
63             order
64                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
65                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT);
66         }
67
68         const userUuid = getUserUuid(api.getState());
69         if (!userUuid) { return; }
70         try {
71             api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
72             const listResults = await this.services.groupsService
73                 .contents(userUuid, {
74                     ...dataExplorerToListParams(dataExplorer),
75                     order: order.getOrder(),
76                     filters,
77                     recursive: true,
78                     includeTrash: true
79                 });
80             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
81
82             const items = listResults.items.map(it => it.uuid);
83
84             api.dispatch(trashPanelActions.SET_ITEMS({
85                 ...listResultsToDataExplorerItemsMeta(listResults),
86                 items
87             }));
88             api.dispatch<any>(updateFavorites(items));
89             api.dispatch<any>(updatePublicFavorites(items));
90             api.dispatch(updateResources(listResults.items));
91         } catch (e) {
92             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
93             api.dispatch(trashPanelActions.SET_ITEMS({
94                 items: [],
95                 itemsAvailable: 0,
96                 page: 0,
97                 rowsPerPage: dataExplorer.rowsPerPage
98             }));
99             api.dispatch(couldNotFetchTrashContents());
100         }
101     }
102 }
103
104 const couldNotFetchTrashContents = () =>
105     snackbarActions.OPEN_SNACKBAR({
106         message: 'Could not fetch trash contents.',
107         kind: SnackbarKind.ERROR
108     });