CR fixes
[arvados.git] / src / store / trash-panel / trash-panel-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import {
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";
26
27 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
28     constructor(private services: ServiceRepository, id: string) {
29         super(id);
30     }
31
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);
37
38         const order = new OrderBuilder<ProjectResource>();
39
40         if (sortColumn) {
41             const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
42                 ? OrderDirection.ASC
43                 : OrderDirection.DESC;
44
45             const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
46             order
47                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
48                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT);
49         }
50
51         const userUuid = this.services.authService.getUuid()!;
52
53         this.services.groupsService
54             .contents(userUuid, {
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)
61                     .getFilters(),
62                 recursive: true,
63                 includeTrash: true
64             })
65             .then((listResults: ListResults<GroupContentsResource>) => {
66                 const items = listResults.items
67                     .filter(it => (it as TrashResource).isTrashed)
68                     .map(it => it.uuid);
69
70                 api.dispatch(trashPanelActions.SET_ITEMS({
71                     ...listResultsToDataExplorerItemsMeta(listResults),
72                     items
73                 }));
74                 api.dispatch<any>(updateFavorites(items));
75                 api.dispatch(updateResources(listResults.items));
76             })
77             .catch(() => {
78                 api.dispatch(couldNotFetchTrashContents());
79             });
80     }
81 }
82
83 const couldNotFetchTrashContents = () =>
84     snackbarActions.OPEN_SNACKBAR({
85         message: 'Could not fetch trash contents.'
86     });