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 { 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 { 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";
31 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
32 constructor(private services: ServiceRepository, id: string) {
36 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
37 const dataExplorer = api.getState().dataExplorer[this.getId()];
38 const columns = dataExplorer.columns as DataColumns<string, CollectionResource>;
40 const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
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)
49 const filters = joinFilters(
54 const userUuid = getUserUuid(api.getState());
55 if (!userUuid) { return; }
57 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
58 const listResults = await this.services.groupsService
60 ...dataExplorerToListParams(dataExplorer),
61 order: getOrder(dataExplorer),
66 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
68 const items = listResults.items.map(it => it.uuid);
70 api.dispatch(trashPanelActions.SET_ITEMS({
71 ...listResultsToDataExplorerItemsMeta(listResults),
74 api.dispatch<any>(updateFavorites(items));
75 api.dispatch<any>(updatePublicFavorites(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 getOrder = (dataExplorer: DataExplorer) => {
91 const sortColumn = getSortColumn<GroupContentsResource>(dataExplorer);
92 const order = new OrderBuilder<GroupContentsResource>();
93 if (sortColumn && sortColumn.sort) {
94 const sortDirection = sortColumn.sort.direction === SortDirection.ASC
96 : OrderDirection.DESC;
98 // Use createdAt as a secondary sort column so we break ties consistently.
100 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.COLLECTION)
101 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.PROJECT)
102 .addOrder(OrderDirection.DESC, "createdAt", GroupContentsResourcePrefix.PROCESS)
105 return order.getOrder();
109 const couldNotFetchTrashContents = () =>
110 snackbarActions.OPEN_SNACKBAR({
111 message: 'Could not fetch trash contents.',
112 kind: SnackbarKind.ERROR