Update filters in views so that the code can be compiled, leave TODO markers to guide...
[arvados-workbench2.git] / src / store / trash-panel / trash-panel-middleware-service.ts
index 3a4da39606ae9a9051883804187e4874ea3f9c22..e6cee25ef559b54233bfb8889dfe7708982fe0e3 100644 (file)
@@ -2,31 +2,37 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { DataExplorerMiddlewareService } from "../data-explorer/data-explorer-middleware-service";
+import {
+    DataExplorerMiddlewareService, dataExplorerToListParams,
+    listResultsToDataExplorerItemsMeta
+} from "../data-explorer/data-explorer-middleware-service";
 import { RootState } from "../store";
 import { DataColumns } from "~/components/data-table/data-table";
 import { ServiceRepository } from "~/services/services";
 import { SortDirection } from "~/components/data-table/data-column";
-import { FilterBuilder } from "~/common/api/filter-builder";
-import { checkPresenceInFavorites } from "../favorites/favorites-actions";
+import { FilterBuilder } from "~/services/api/filter-builder";
 import { trashPanelActions } from "./trash-panel-action";
 import { Dispatch, MiddlewareAPI } from "redux";
-import { OrderBuilder, OrderDirection } from "~/common/api/order-builder";
+import { OrderBuilder, OrderDirection } from "~/services/api/order-builder";
 import { GroupContentsResourcePrefix } from "~/services/groups-service/groups-service";
-import { resourceToDataItem, TrashPanelItem } from "~/views/trash-panel/trash-panel-item";
 import { TrashPanelColumnNames, TrashPanelFilter } from "~/views/trash-panel/trash-panel";
 import { ProjectResource } from "~/models/project";
 import { ProjectPanelColumnNames } from "~/views/project-panel/project-panel";
+import { updateFavorites } from "~/store/favorites/favorites-actions";
+import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
+import { updateResources } from "~/store/resources/resources-actions";
+import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
+import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
 
 export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
     constructor(private services: ServiceRepository, id: string) {
         super(id);
     }
 
-    requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
+    async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
         const dataExplorer = api.getState().dataExplorer[this.getId()];
-        const columns = dataExplorer.columns as DataColumns<TrashPanelItem, TrashPanelFilter>;
-        const sortColumn = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
+        const columns = dataExplorer.columns as DataColumns<string>;
+        const sortColumn = getSortColumn(dataExplorer);
         const typeFilters = this.getColumnFilters(columns, TrashPanelColumnNames.TYPE);
 
         const order = new OrderBuilder<ProjectResource>();
@@ -42,37 +48,49 @@ export class TrashPanelMiddlewareService extends DataExplorerMiddlewareService {
                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT);
         }
 
-        const userUuid = this.services.authService.getUuid()!;
+        try {
+            api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
+            const userUuid = this.services.authService.getUuid()!;
+            const listResults = await this.services.groupsService
+                .contents(userUuid, {
+                    ...dataExplorerToListParams(dataExplorer),
+                    order: order.getOrder(),
+                    filters: new FilterBuilder()
+                        // TODO: update filters
+                        // .addIsA("uuid", typeFilters.map(f => f.type))
+                        .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
+                        .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
+                        .addEqual("is_trashed", true)
+                        .getFilters(),
+                    recursive: true,
+                    includeTrash: true
+                });
+            api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
 
-        this.services.groupsService
-            .contents(userUuid, {
-                limit: dataExplorer.rowsPerPage,
-                offset: dataExplorer.page * dataExplorer.rowsPerPage,
-                order: order.getOrder(),
-                filters: new FilterBuilder()
-                    .addIsA("uuid", typeFilters.map(f => f.type))
-                    .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
-                    .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
-                    .getFilters(),
-                recursive: true,
-                includeTrash: true
-            })
-            .then(response => {
-                api.dispatch(trashPanelActions.SET_ITEMS({
-                    items: response.items.map(resourceToDataItem).filter(it => it.isTrashed),
-                    itemsAvailable: response.itemsAvailable,
-                    page: Math.floor(response.offset / response.limit),
-                    rowsPerPage: response.limit
-                }));
-                api.dispatch<any>(checkPresenceInFavorites(response.items.map(item => item.uuid)));
-            })
-            .catch(() => {
-                api.dispatch(trashPanelActions.SET_ITEMS({
-                    items: [],
-                    itemsAvailable: 0,
-                    page: 0,
-                    rowsPerPage: dataExplorer.rowsPerPage
-                }));
-            });
+            const items = listResults.items.map(it => it.uuid);
+
+            api.dispatch(trashPanelActions.SET_ITEMS({
+                ...listResultsToDataExplorerItemsMeta(listResults),
+                items
+            }));
+            api.dispatch<any>(updateFavorites(items));
+            api.dispatch(updateResources(listResults.items));
+        } catch (e) {
+            api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
+            api.dispatch(trashPanelActions.SET_ITEMS({
+                items: [],
+                itemsAvailable: 0,
+                page: 0,
+                rowsPerPage: dataExplorer.rowsPerPage
+            }));
+            api.dispatch(couldNotFetchTrashContents());
+        }
     }
 }
+
+const couldNotFetchTrashContents = () =>
+    snackbarActions.OPEN_SNACKBAR({
+        message: 'Could not fetch trash contents.',
+        kind: SnackbarKind.ERROR
+    });
+