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 { 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 { TrashPanelColumnNames } from "views/trash-panel/trash-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 import { ContextMenuActionNames } from "views-components/context-menu/context-menu-action-set";
31 import { removeDisabledButton } from "store/multiselect/multiselect-actions";
32 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
33 constructor(private services: ServiceRepository, id: string) {
37 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
38 const dataExplorer = api.getState().dataExplorer[this.getId()];
39 const columns = dataExplorer.columns as DataColumns<string, CollectionResource>;
41 const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, TrashPanelColumnNames.TYPE));
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)
50 const filters = joinFilters(
55 const userUuid = getUserUuid(api.getState());
56 if (!userUuid) { return; }
58 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
59 const listResults = await this.services.groupsService
61 ...dataExplorerToListParams(dataExplorer),
62 order: getOrder(dataExplorer),
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<any>(updatePublicFavorites(items));
77 api.dispatch(updateResources(listResults.items));
79 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
80 api.dispatch(trashPanelActions.SET_ITEMS({
84 rowsPerPage: dataExplorer.rowsPerPage
86 api.dispatch(couldNotFetchTrashContents());
88 api.dispatch<any>(removeDisabledButton(ContextMenuActionNames.MOVE_TO_TRASH))
92 const getOrder = (dataExplorer: DataExplorer) => {
93 const sortColumn = getSortColumn<GroupContentsResource>(dataExplorer);
94 const order = new OrderBuilder<GroupContentsResource>();
95 if (sortColumn && sortColumn.sort) {
96 const sortDirection = sortColumn.sort.direction === SortDirection.ASC
98 : OrderDirection.DESC;
100 // Use createdAt as a secondary sort column so we break ties consistently.
102 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.COLLECTION)
103 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.PROJECT)
104 .addOrder(OrderDirection.DESC, "createdAt", GroupContentsResourcePrefix.PROCESS)
107 return order.getOrder();
111 const couldNotFetchTrashContents = () =>
112 snackbarActions.OPEN_SNACKBAR({
113 message: 'Could not fetch trash contents.',
114 kind: SnackbarKind.ERROR