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 { ServiceRepository } from "services/services";
12 import { DataColumns, 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 { ContentsArguments, GroupContentsResource, GroupContentsResourcePrefix } from "services/groups-service/groups-service";
18 import { TrashPanelColumnNames } from "views/trash-panel/trash-panel";
19 import { updateFavorites } from "store/favorites/favorites-actions";
20 import { updatePublicFavorites } from 'store/public-favorites/public-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 { DataExplorer, getDataExplorer, 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 import { CollectionResource } from "models/collection";
29 import { ContextMenuActionNames } from "views-components/context-menu/context-menu-action-set";
30 import { removeDisabledButton } from "store/multiselect/multiselect-actions";
31 import { couldNotFetchItemsAvailable } from "store/data-explorer/data-explorer-action";
32 import { ListResults } from "services/common-service/common-service";
33 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
34 constructor(private services: ServiceRepository, id: string) {
38 async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
39 const state = api.getState();
40 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
42 const userUuid = getUserUuid(api.getState());
43 if (!userUuid) { return; }
45 if (!background) { api.dispatch(progressIndicatorActions.START_WORKING(this.getId())); }
48 const listResults = await this.services.groupsService
49 .contents('', getParams(dataExplorer));
51 const items = listResults.items.map(it => it.uuid);
52 api.dispatch(trashPanelActions.SET_ITEMS({
53 ...listResultsToDataExplorerItemsMeta(listResults),
56 api.dispatch<any>(updateFavorites(items));
57 api.dispatch<any>(updatePublicFavorites(items));
58 api.dispatch(updateResources(listResults.items));
60 api.dispatch(trashPanelActions.SET_ITEMS({
64 rowsPerPage: dataExplorer.rowsPerPage
66 api.dispatch(couldNotFetchTrashContents());
68 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
70 api.dispatch<any>(removeDisabledButton(ContextMenuActionNames.MOVE_TO_TRASH))
73 async requestCount(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
74 const state = api.getState();
75 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
77 if (criteriaChanged) {
79 return this.services.groupsService.contents('', getCountParams(dataExplorer))
80 .then((results: ListResults<GroupContentsResource>) => {
81 if (results.itemsAvailable !== undefined) {
82 api.dispatch<any>(trashPanelActions.SET_ITEMS_AVAILABLE(results.itemsAvailable));
84 couldNotFetchItemsAvailable();
91 const getOrder = (dataExplorer: DataExplorer) => {
92 const sortColumn = getSortColumn<GroupContentsResource>(dataExplorer);
93 const order = new OrderBuilder<GroupContentsResource>();
94 if (sortColumn && sortColumn.sort) {
95 const sortDirection = sortColumn.sort.direction === SortDirection.ASC
97 : OrderDirection.DESC;
99 // Use createdAt as a secondary sort column so we break ties consistently.
101 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.COLLECTION)
102 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.PROJECT)
103 .addOrder(OrderDirection.DESC, "createdAt", GroupContentsResourcePrefix.PROCESS)
106 return order.getOrder();
110 const getFilters = (dataExplorer: DataExplorer) => {
111 const columns = dataExplorer.columns as DataColumns<string, CollectionResource>;
112 const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, TrashPanelColumnNames.TYPE));
114 const otherFilters = new FilterBuilder()
115 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
116 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
117 .addEqual("is_trashed", true)
126 const getParams = (dataExplorer: DataExplorer): ContentsArguments => ({
127 ...dataExplorerToListParams(dataExplorer),
128 order: getOrder(dataExplorer),
129 filters: getFilters(dataExplorer),
135 const getCountParams = (dataExplorer: DataExplorer): ContentsArguments => ({
136 filters: getFilters(dataExplorer),
143 const couldNotFetchTrashContents = () =>
144 snackbarActions.OPEN_SNACKBAR({
145 message: 'Could not fetch trash contents.',
146 kind: SnackbarKind.ERROR