refs #14926-modal-with-inputs-is-empty
[arvados.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 { DataColumns } from "~/components/data-table/data-table";
11 import { ServiceRepository } from "~/services/services";
12 import { SortDirection } from "~/components/data-table/data-column";
13 import { FilterBuilder } from "~/services/api/filter-builder";
14 import { trashPanelActions } from "./trash-panel-action";
15 import { Dispatch, MiddlewareAPI } from "redux";
16 import { OrderBuilder, OrderDirection } from "~/services/api/order-builder";
17 import { GroupContentsResourcePrefix } from "~/services/groups-service/groups-service";
18 import { ProjectResource } from "~/models/project";
19 import { ProjectPanelColumnNames } from "~/views/project-panel/project-panel";
20 import { updateFavorites } from "~/store/favorites/favorites-actions";
21 import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
22 import { updateResources } from "~/store/resources/resources-actions";
23 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
24 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
25 import { serializeResourceTypeFilters } from '~/store//resource-type-filters/resource-type-filters';
26 import { getDataExplorerColumnFilters } from '~/store/data-explorer/data-explorer-middleware-service';
27 import { joinFilters } from '~/services/api/filter-builder';
28
29 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
30     constructor(private services: ServiceRepository, id: string) {
31         super(id);
32     }
33
34     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
35         const dataExplorer = api.getState().dataExplorer[this.getId()];
36         const columns = dataExplorer.columns as DataColumns<string>;
37         const sortColumn = getSortColumn(dataExplorer);
38
39         const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
40
41         const otherFilters = new FilterBuilder()
42             .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
43             // .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROCESS)
44             .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
45             .addEqual("is_trashed", true)
46             .getFilters();
47
48         const filters = joinFilters(
49             typeFilters,
50             otherFilters,
51         );
52
53         const order = new OrderBuilder<ProjectResource>();
54
55         if (sortColumn) {
56             const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
57                 ? OrderDirection.ASC
58                 : OrderDirection.DESC;
59
60             const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
61             order
62                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
63                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT);
64         }
65
66         try {
67             api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
68             const userUuid = this.services.authService.getUuid()!;
69             const listResults = await this.services.groupsService
70                 .contents(userUuid, {
71                     ...dataExplorerToListParams(dataExplorer),
72                     order: order.getOrder(),
73                     filters,
74                     recursive: true,
75                     includeTrash: true
76                 });
77             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
78
79             const items = listResults.items.map(it => it.uuid);
80
81             api.dispatch(trashPanelActions.SET_ITEMS({
82                 ...listResultsToDataExplorerItemsMeta(listResults),
83                 items
84             }));
85             api.dispatch<any>(updateFavorites(items));
86             api.dispatch(updateResources(listResults.items));
87         } catch (e) {
88             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
89             api.dispatch(trashPanelActions.SET_ITEMS({
90                 items: [],
91                 itemsAvailable: 0,
92                 page: 0,
93                 rowsPerPage: dataExplorer.rowsPerPage
94             }));
95             api.dispatch(couldNotFetchTrashContents());
96         }
97     }
98 }
99
100 const couldNotFetchTrashContents = () =>
101     snackbarActions.OPEN_SNACKBAR({
102         message: 'Could not fetch trash contents.',
103         kind: SnackbarKind.ERROR
104     });
105