21021: Merge branch 'main' into 21021-controller-logout
[arvados.git] / services / workbench2 / src / store / trash / trash-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from "store/store";
7 import { ServiceRepository } from "services/services";
8 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
9 import { trashPanelActions } from "store/trash-panel/trash-panel-action";
10 import { activateSidePanelTreeItem, loadSidePanelTreeProjects } from "store/side-panel-tree/side-panel-tree-actions";
11 import { projectPanelActions } from "store/project-panel/project-panel-action-bind";
12 import { sharedWithMePanelActions } from "store/shared-with-me-panel/shared-with-me-panel-actions";
13 import { ResourceKind } from "models/resource";
14 import { navigateTo, navigateToTrash } from "store/navigation/navigation-action";
15 import { matchCollectionRoute, matchSharedWithMeRoute } from "routes/routes";
16
17 export const toggleProjectTrashed =
18     (uuid: string, ownerUuid: string, isTrashed: boolean, isMulti: boolean) =>
19         async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
20             let errorMessage = "";
21             let successMessage = "";
22             let untrashedResource;
23             try {
24                 if (isTrashed) {
25                     errorMessage = "Could not restore project from trash";
26                     successMessage = "Restored project from trash";
27                     untrashedResource = await services.groupsService.untrash(uuid);
28                     dispatch<any>(isMulti || !untrashedResource ? navigateToTrash : navigateTo(uuid));
29                     dispatch<any>(activateSidePanelTreeItem(uuid));
30                 } else {
31                     errorMessage = "Could not move project to trash";
32                     successMessage = "Added project to trash";
33                     await services.groupsService.trash(uuid);
34                     dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
35
36                     const { location } = getState().router;
37                     if (matchSharedWithMeRoute(location ? location.pathname : "")) {
38                         dispatch(sharedWithMePanelActions.REQUEST_ITEMS());
39                     }
40                     else {
41                         dispatch<any>(navigateTo(ownerUuid));
42                     }
43                 }
44                 if (untrashedResource) {
45                     dispatch(
46                         snackbarActions.OPEN_SNACKBAR({
47                             message: successMessage,
48                             hideDuration: 2000,
49                             kind: SnackbarKind.SUCCESS,
50                         })
51                     );
52                 }
53             } catch (e) {
54                 if (e.status === 422) {
55                     dispatch(
56                         snackbarActions.OPEN_SNACKBAR({
57                             message: "Could not restore project from trash: Duplicate name at destination",
58                             kind: SnackbarKind.ERROR,
59                         })
60                     );
61                 } else {
62                     dispatch(
63                         snackbarActions.OPEN_SNACKBAR({
64                             message: errorMessage,
65                             kind: SnackbarKind.ERROR,
66                         })
67                     );
68                 }
69             }
70         };
71
72 export const toggleCollectionTrashed =
73     (uuid: string, isTrashed: boolean) =>
74         async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
75             let errorMessage = "";
76             let successMessage = "";
77             try {
78                 if (isTrashed) {
79                     const { location } = getState().router;
80                     errorMessage = "Could not restore collection from trash";
81                     successMessage = "Restored from trash";
82                     await services.collectionService.untrash(uuid);
83                     if (matchCollectionRoute(location ? location.pathname : "")) {
84                         dispatch(navigateToTrash);
85                     }
86                     dispatch(trashPanelActions.REQUEST_ITEMS());
87                 } else {
88                     errorMessage = "Could not move collection to trash";
89                     successMessage = "Added to trash";
90                     await services.collectionService.trash(uuid);
91                     dispatch(projectPanelActions.REQUEST_ITEMS());
92                 }
93                 dispatch(
94                     snackbarActions.OPEN_SNACKBAR({
95                         message: successMessage,
96                         hideDuration: 2000,
97                         kind: SnackbarKind.SUCCESS,
98                     })
99                 );
100             } catch (e) {
101                 if (e.status === 422) {
102                     dispatch(
103                         snackbarActions.OPEN_SNACKBAR({
104                             message: "Could not restore collection from trash: Duplicate name at destination",
105                             kind: SnackbarKind.ERROR,
106                         })
107                     );
108                 } else {
109                     dispatch(
110                         snackbarActions.OPEN_SNACKBAR({
111                             message: errorMessage,
112                             kind: SnackbarKind.ERROR,
113                         })
114                     );
115                 }
116             }
117         };
118
119 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) => (dispatch: Dispatch) => {
120     if (kind === ResourceKind.PROJECT) {
121         dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!, false));
122     } else if (kind === ResourceKind.COLLECTION) {
123         dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));
124     }
125 };