refs #14915-after-move-to-action-inside-tree-panel-the-tree-is-not-refreshed
[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 { 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 { 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
30 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
31     constructor(private services: ServiceRepository, id: string) {
32         super(id);
33     }
34
35     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
36         const dataExplorer = api.getState().dataExplorer[this.getId()];
37         const columns = dataExplorer.columns as DataColumns<string>;
38         const sortColumn = getSortColumn(dataExplorer);
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 order = new OrderBuilder<ProjectResource>();
55
56         if (sortColumn) {
57             const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
58                 ? OrderDirection.ASC
59                 : OrderDirection.DESC;
60
61             const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
62             order
63                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
64                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT);
65         }
66
67         try {
68             api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
69             const userUuid = this.services.authService.getUuid()!;
70             const listResults = await this.services.groupsService
71                 .contents(userUuid, {
72                     ...dataExplorerToListParams(dataExplorer),
73                     order: order.getOrder(),
74                     filters,
75                     recursive: true,
76                     includeTrash: true
77                 });
78             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
79
80             const items = listResults.items.map(it => it.uuid);
81
82             api.dispatch(trashPanelActions.SET_ITEMS({
83                 ...listResultsToDataExplorerItemsMeta(listResults),
84                 items
85             }));
86             api.dispatch<any>(updateFavorites(items));
87             api.dispatch<any>(updatePublicFavorites(items));
88             api.dispatch(updateResources(listResults.items));
89         } catch (e) {
90             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
91             api.dispatch(trashPanelActions.SET_ITEMS({
92                 items: [],
93                 itemsAvailable: 0,
94                 page: 0,
95                 rowsPerPage: dataExplorer.rowsPerPage
96             }));
97             api.dispatch(couldNotFetchTrashContents());
98         }
99     }
100 }
101
102 const couldNotFetchTrashContents = () =>
103     snackbarActions.OPEN_SNACKBAR({
104         message: 'Could not fetch trash contents.',
105         kind: SnackbarKind.ERROR
106     });
107