1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 { TrashPanelColumnNames, TrashPanelFilter } from "~/views/trash-panel/trash-panel";
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 { 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";
27 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
28 constructor(private services: ServiceRepository, id: string) {
32 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
33 const dataExplorer = api.getState().dataExplorer[this.getId()];
34 const columns = dataExplorer.columns as DataColumns<string, TrashPanelFilter>;
35 const sortColumn = getSortColumn(dataExplorer);
36 const typeFilters = this.getColumnFilters(columns, TrashPanelColumnNames.TYPE);
38 const order = new OrderBuilder<ProjectResource>();
41 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
43 : OrderDirection.DESC;
45 const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
47 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
48 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT);
52 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
53 const userUuid = this.services.authService.getUuid()!;
54 const listResults = await this.services.groupsService
56 ...dataExplorerToListParams(dataExplorer),
57 order: order.getOrder(),
58 filters: new FilterBuilder()
59 .addIsA("uuid", typeFilters.map(f => f.type))
60 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
61 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
62 .addEqual("is_trashed", true)
67 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
69 const items = listResults.items.map(it => it.uuid);
71 api.dispatch(trashPanelActions.SET_ITEMS({
72 ...listResultsToDataExplorerItemsMeta(listResults),
75 api.dispatch<any>(updateFavorites(items));
76 api.dispatch(updateResources(listResults.items));
78 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
79 api.dispatch(trashPanelActions.SET_ITEMS({
83 rowsPerPage: dataExplorer.rowsPerPage
85 api.dispatch(couldNotFetchTrashContents());
90 const couldNotFetchTrashContents = () =>
91 snackbarActions.OPEN_SNACKBAR({
92 message: 'Could not fetch trash contents.',
93 kind: SnackbarKind.ERROR