Merge branch '21999-packer-fixes'
[arvados.git] / services / workbench2 / 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 { 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) {
35         super(id);
36     }
37
38     async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
39         const state = api.getState();
40         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
41
42         const userUuid = getUserUuid(api.getState());
43         if (!userUuid) { return; }
44         try {
45             if (!background) { api.dispatch(progressIndicatorActions.START_WORKING(this.getId())); }
46
47             // Get items
48             const listResults = await this.services.groupsService
49                 .contents('', getParams(dataExplorer));
50
51             const items = listResults.items.map(it => it.uuid);
52             api.dispatch(trashPanelActions.SET_ITEMS({
53                 ...listResultsToDataExplorerItemsMeta(listResults),
54                 items
55             }));
56             api.dispatch<any>(updateFavorites(items));
57             api.dispatch<any>(updatePublicFavorites(items));
58             api.dispatch(updateResources(listResults.items));
59         } catch (e) {
60             api.dispatch(trashPanelActions.SET_ITEMS({
61                 items: [],
62                 itemsAvailable: 0,
63                 page: 0,
64                 rowsPerPage: dataExplorer.rowsPerPage
65             }));
66             api.dispatch(couldNotFetchTrashContents());
67         } finally {
68             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
69         }
70         api.dispatch<any>(removeDisabledButton(ContextMenuActionNames.MOVE_TO_TRASH))
71     }
72
73     async requestCount(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
74         const state = api.getState();
75         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
76
77         if (criteriaChanged) {
78             // Get itemsAvailable
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));
83                     } else {
84                         couldNotFetchItemsAvailable();
85                     }
86                 });
87         }
88     }
89 }
90
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
96             ? OrderDirection.ASC
97             : OrderDirection.DESC;
98
99         // Use createdAt as a secondary sort column so we break ties consistently.
100         return order
101             .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.COLLECTION)
102             .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.PROJECT)
103             .addOrder(OrderDirection.DESC, "createdAt", GroupContentsResourcePrefix.PROCESS)
104             .getOrder();
105     } else {
106         return order.getOrder();
107     }
108 };
109
110 const getFilters = (dataExplorer: DataExplorer) => {
111     const columns = dataExplorer.columns as DataColumns<string, CollectionResource>;
112     const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, TrashPanelColumnNames.TYPE));
113
114     const otherFilters = new FilterBuilder()
115         .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
116         .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
117         .addEqual("is_trashed", true)
118         .getFilters();
119
120     return joinFilters(
121         typeFilters,
122         otherFilters,
123     );
124 };
125
126 const getParams = (dataExplorer: DataExplorer): ContentsArguments => ({
127     ...dataExplorerToListParams(dataExplorer),
128     order: getOrder(dataExplorer),
129     filters: getFilters(dataExplorer),
130     recursive: true,
131     includeTrash: true,
132     count: 'none',
133 });
134
135 const getCountParams = (dataExplorer: DataExplorer): ContentsArguments => ({
136     filters: getFilters(dataExplorer),
137     recursive: true,
138     includeTrash: true,
139     limit: 0,
140     count: 'exact',
141 });
142
143 const couldNotFetchTrashContents = () =>
144     snackbarActions.OPEN_SNACKBAR({
145         message: 'Could not fetch trash contents.',
146         kind: SnackbarKind.ERROR
147     });