15768: fixed 422 handling on untrash Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
authorLisa Knox <lisaknox83@gmail.com>
Wed, 4 Oct 2023 16:19:43 +0000 (12:19 -0400)
committerLisa Knox <lisaknox83@gmail.com>
Wed, 4 Oct 2023 16:19:43 +0000 (12:19 -0400)
src/services/common-service/trashable-resource-service.ts
src/store/trash/trash-actions.ts

index 4d6b130b906d68a1f68c4e96e7e50a7b0fbeee9c..5e4704b64d7020852e5d314b18bb3270403c8fb8 100644 (file)
@@ -9,29 +9,25 @@ import { CommonResourceService } from "services/common-service/common-resource-s
 import { ApiActions } from "services/api/api-actions";
 
 export class TrashableResourceService<T extends TrashableResource> extends CommonResourceService<T> {
-
     constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
         super(serverApi, resourceType, actions, readOnlyFields);
     }
 
     trash(uuid: string): Promise<T> {
-        return CommonResourceService.defaultResponse(
-            this.serverApi
-                .post(this.resourceType + `/${uuid}/trash`),
-            this.actions
-        );
+        return CommonResourceService.defaultResponse(this.serverApi.post(this.resourceType + `/${uuid}/trash`), this.actions);
     }
 
     untrash(uuid: string): Promise<T> {
         const params = {
-            ensure_unique_name: true
+            ensure_unique_name: true,
         };
         return CommonResourceService.defaultResponse(
-            this.serverApi
-                .post(this.resourceType + `/${uuid}/untrash`, {
-                    params: CommonResourceService.mapKeys(snakeCase)(params)
-                }),
-            this.actions
+            this.serverApi.post(this.resourceType + `/${uuid}/untrash`, {
+                params: CommonResourceService.mapKeys(snakeCase)(params),
+            }),
+            this.actions,
+            undefined,
+            false
         );
     }
 }
index ffdad04a5a74badf6a8d651ba091886541b50553..3f8f8346e7b3161b6ce204183def4834872acc2d 100644 (file)
@@ -18,12 +18,13 @@ export const toggleProjectTrashed =
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
         let errorMessage = "";
         let successMessage = "";
+        let untrashedResource;
         try {
             if (isTrashed) {
                 errorMessage = "Could not restore project from trash";
                 successMessage = "Restored project from trash";
-                await services.groupsService.untrash(uuid);
-                dispatch<any>(isMulti ? navigateToTrash : navigateTo(uuid));
+                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";
@@ -32,20 +33,31 @@ export const toggleProjectTrashed =
                 dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
                 dispatch<any>(navigateTo(ownerUuid));
             }
-            dispatch(
-                snackbarActions.OPEN_SNACKBAR({
-                    message: successMessage,
-                    hideDuration: 2000,
-                    kind: SnackbarKind.SUCCESS,
-                })
-            );
+            if (untrashedResource) {
+                dispatch(
+                    snackbarActions.OPEN_SNACKBAR({
+                        message: successMessage,
+                        hideDuration: 2000,
+                        kind: SnackbarKind.SUCCESS,
+                    })
+                );
+            }
         } catch (e) {
-            dispatch(
-                snackbarActions.OPEN_SNACKBAR({
-                    message: errorMessage,
-                    kind: SnackbarKind.ERROR,
-                })
-            );
+            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,
+                    })
+                );
+            }
         }
     };
 
@@ -78,12 +90,21 @@ export const toggleCollectionTrashed =
                 })
             );
         } catch (e) {
-            dispatch(
-                snackbarActions.OPEN_SNACKBAR({
-                    message: errorMessage,
-                    kind: SnackbarKind.ERROR,
-                })
-            );
+            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,
+                    })
+                );
+            }
         }
     };