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 "~/common/api/filter-builder";
14 import { trashPanelActions } from "./trash-panel-action";
15 import { Dispatch, MiddlewareAPI } from "redux";
16 import { OrderBuilder, OrderDirection } from "~/common/api/order-builder";
17 import { GroupContentsResource, 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 { TrashResource } from "~/models/resource";
23 import { ListResults } from "~/common/api/common-resource-service";
24 import { snackbarActions } from "~/store/snackbar/snackbar-actions";
25 import { updateResources } from "~/store/resources/resources-actions";
27 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
28 constructor(private services: ServiceRepository, id: string) {
32 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 = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
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);
51 const userUuid = this.services.authService.getUuid()!;
53 this.services.groupsService
55 ...dataExplorerToListParams(dataExplorer),
56 order: order.getOrder(),
57 filters: new FilterBuilder()
58 .addIsA("uuid", typeFilters.map(f => f.type))
59 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
60 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
65 .then((listResults: ListResults<GroupContentsResource>) => {
66 const items = listResults.items
67 .filter(it => (it as TrashResource).isTrashed)
70 api.dispatch(trashPanelActions.SET_ITEMS({
71 ...listResultsToDataExplorerItemsMeta(listResults),
74 api.dispatch<any>(updateFavorites(items));
75 api.dispatch(updateResources(listResults.items));
78 api.dispatch(couldNotFetchTrashContents());
83 const couldNotFetchTrashContents = () =>
84 snackbarActions.OPEN_SNACKBAR({
85 message: 'Could not fetch trash contents.'