15768: made separate file for project panel action binding Arvados-DCO-1.1-Signed...
[arvados.git] / src / store / trash / trash-actions.ts
index 97d81853d0a6a5eb740548c4290787d37a3f2514..884293a90e57e4e914486fe864b9bb4ea9240473 100644 (file)
 // SPDX-License-Identifier: AGPL-3.0
 
 import { Dispatch } from "redux";
-import { RootState } from "~/store/store";
-import { ServiceRepository } from "~/services/services";
-import { snackbarActions } from "~/store/snackbar/snackbar-actions";
-import { trashPanelActions } from "~/store/trash-panel/trash-panel-action";
+import { RootState } from "store/store";
+import { ServiceRepository } from "services/services";
+import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
+import { trashPanelActions } from "store/trash-panel/trash-panel-action";
+import { activateSidePanelTreeItem, loadSidePanelTreeProjects } from "store/side-panel-tree/side-panel-tree-actions";
+import { projectPanelActions } from "store/project-panel/project-panel-action-bind";
+import { ResourceKind } from "models/resource";
+import { navigateTo, navigateToTrash } from "store/navigation/navigation-action";
+import { matchCollectionRoute } from "routes/routes";
 
-export const toggleProjectTrashed = (resource: { uuid: string; name: string, isTrashed?: boolean, ownerUuid?: string }) =>
+export const toggleProjectTrashed =
+    (uuid: string, ownerUuid: string, isTrashed: boolean, isMulti: boolean) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
-        dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Working..." }));
-        if (resource.isTrashed) {
-            return services.groupsService.untrash(resource.uuid).then(() => {
-                // dispatch<any>(getProjectList(resource.ownerUuid)).then(() => {
-                //     dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelId.PROJECTS));
-                //     dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN({ itemId: resource.ownerUuid!!, open: true, recursive: true }));
-                // });
-                dispatch(trashPanelActions.REQUEST_ITEMS());
-                dispatch(snackbarActions.CLOSE_SNACKBAR());
-                dispatch(snackbarActions.OPEN_SNACKBAR({
-                    message: "Restored from trash",
-                    hideDuration: 2000
-                }));
-            });
-        } else {
-            return services.groupsService.trash(resource.uuid).then(() => {
-                // dispatch<any>(getProjectList(resource.ownerUuid)).then(() => {
-                //     dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN({ itemId: resource.ownerUuid!!, open: true, recursive: true }));
-                // });
-                dispatch(snackbarActions.CLOSE_SNACKBAR());
-                dispatch(snackbarActions.OPEN_SNACKBAR({
-                    message: "Added to trash",
-                    hideDuration: 2000
-                }));
-            });
+        let errorMessage = "";
+        let successMessage = "";
+        let untrashedResource;
+        try {
+            if (isTrashed) {
+                errorMessage = "Could not restore project from trash";
+                successMessage = "Restored project from trash";
+                untrashedResource = await services.groupsService.untrash(uuid);
+                dispatch<any>(isMulti || !untrashedResource ? navigateToTrash : navigateTo(uuid));
+                dispatch<any>(activateSidePanelTreeItem(uuid));
+            } else {
+                errorMessage = "Could not move project to trash";
+                successMessage = "Added project to trash";
+                await services.groupsService.trash(uuid);
+                dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
+                dispatch<any>(navigateTo(ownerUuid));
+            }
+            if (untrashedResource) {
+                dispatch(
+                    snackbarActions.OPEN_SNACKBAR({
+                        message: successMessage,
+                        hideDuration: 2000,
+                        kind: SnackbarKind.SUCCESS,
+                    })
+                );
+            }
+        } catch (e) {
+            if (e.status === 422) {
+                dispatch(
+                    snackbarActions.OPEN_SNACKBAR({
+                        message: "Could not restore project from trash: Duplicate name at destination",
+                        kind: SnackbarKind.ERROR,
+                    })
+                );
+            } else {
+                dispatch(
+                    snackbarActions.OPEN_SNACKBAR({
+                        message: errorMessage,
+                        kind: SnackbarKind.ERROR,
+                    })
+                );
+            }
         }
     };
 
-export const toggleCollectionTrashed = (resource: { uuid: string; name: string, isTrashed?: boolean, ownerUuid?: string }) =>
+export const toggleCollectionTrashed =
+    (uuid: string, isTrashed: boolean) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
-        dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Working..." }));
-        if (resource.isTrashed) {
-            return services.collectionService.untrash(resource.uuid).then(() => {
-                // dispatch<any>(getProjectList(resource.ownerUuid)).then(() => {
-                //     dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelId.PROJECTS));
-                //     dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN({ itemId: resource.ownerUuid!!, open: true, recursive: true }));
-                // });
+        let errorMessage = "";
+        let successMessage = "";
+        try {
+            if (isTrashed) {
+                const { location } = getState().router;
+                errorMessage = "Could not restore collection from trash";
+                successMessage = "Restored from trash";
+                await services.collectionService.untrash(uuid);
+                if (matchCollectionRoute(location ? location.pathname : "")) {
+                    dispatch(navigateToTrash);
+                }
                 dispatch(trashPanelActions.REQUEST_ITEMS());
-                dispatch(snackbarActions.CLOSE_SNACKBAR());
-                dispatch(snackbarActions.OPEN_SNACKBAR({
-                    message: "Restored from trash",
-                    hideDuration: 2000
-                }));
-            });
-        } else {
-            return services.collectionService.trash(resource.uuid).then(() => {
-                dispatch(snackbarActions.CLOSE_SNACKBAR());
-                dispatch(snackbarActions.OPEN_SNACKBAR({
-                    message: "Added to trash",
-                    hideDuration: 2000
-                }));
-            });
+            } else {
+                errorMessage = "Could not move collection to trash";
+                successMessage = "Added to trash";
+                await services.collectionService.trash(uuid);
+                dispatch(projectPanelActions.REQUEST_ITEMS());
+            }
+            dispatch(
+                snackbarActions.OPEN_SNACKBAR({
+                    message: successMessage,
+                    hideDuration: 2000,
+                    kind: SnackbarKind.SUCCESS,
+                })
+            );
+        } catch (e) {
+            if (e.status === 422) {
+                dispatch(
+                    snackbarActions.OPEN_SNACKBAR({
+                        message: "Could not restore collection from trash: Duplicate name at destination",
+                        kind: SnackbarKind.ERROR,
+                    })
+                );
+            } else {
+                dispatch(
+                    snackbarActions.OPEN_SNACKBAR({
+                        message: errorMessage,
+                        kind: SnackbarKind.ERROR,
+                    })
+                );
+            }
         }
     };
+
+export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) => (dispatch: Dispatch) => {
+    if (kind === ResourceKind.PROJECT) {
+        dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!, false));
+    } else if (kind === ResourceKind.COLLECTION) {
+        dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));
+    }
+};