Merge branch '15768-multi-select-operations'
[arvados-workbench2.git] / 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 { ResourceKind } from "models/resource";
13 import { navigateTo, navigateToTrash } from "store/navigation/navigation-action";
14 import { matchCollectionRoute } from "routes/routes";
15
16 export const toggleProjectTrashed =
17     (uuid: string, ownerUuid: string, isTrashed: boolean, isMulti: boolean) =>
18     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
19         let errorMessage = "";
20         let successMessage = "";
21         let untrashedResource;
22         try {
23             if (isTrashed) {
24                 errorMessage = "Could not restore project from trash";
25                 successMessage = "Restored project from trash";
26                 untrashedResource = await services.groupsService.untrash(uuid);
27                 dispatch<any>(isMulti || !untrashedResource ? navigateToTrash : navigateTo(uuid));
28                 dispatch<any>(activateSidePanelTreeItem(uuid));
29             } else {
30                 errorMessage = "Could not move project to trash";
31                 successMessage = "Added project to trash";
32                 await services.groupsService.trash(uuid);
33                 dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
34                 dispatch<any>(navigateTo(ownerUuid));
35             }
36             if (untrashedResource) {
37                 dispatch(
38                     snackbarActions.OPEN_SNACKBAR({
39                         message: successMessage,
40                         hideDuration: 2000,
41                         kind: SnackbarKind.SUCCESS,
42                     })
43                 );
44             }
45         } catch (e) {
46             if (e.status === 422) {
47                 dispatch(
48                     snackbarActions.OPEN_SNACKBAR({
49                         message: "Could not restore project from trash: Duplicate name at destination",
50                         kind: SnackbarKind.ERROR,
51                     })
52                 );
53             } else {
54                 dispatch(
55                     snackbarActions.OPEN_SNACKBAR({
56                         message: errorMessage,
57                         kind: SnackbarKind.ERROR,
58                     })
59                 );
60             }
61         }
62     };
63
64 export const toggleCollectionTrashed =
65     (uuid: string, isTrashed: boolean) =>
66     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
67         let errorMessage = "";
68         let successMessage = "";
69         try {
70             if (isTrashed) {
71                 const { location } = getState().router;
72                 errorMessage = "Could not restore collection from trash";
73                 successMessage = "Restored from trash";
74                 await services.collectionService.untrash(uuid);
75                 if (matchCollectionRoute(location ? location.pathname : "")) {
76                     dispatch(navigateToTrash);
77                 }
78                 dispatch(trashPanelActions.REQUEST_ITEMS());
79             } else {
80                 errorMessage = "Could not move collection to trash";
81                 successMessage = "Added to trash";
82                 await services.collectionService.trash(uuid);
83                 dispatch(projectPanelActions.REQUEST_ITEMS());
84             }
85             dispatch(
86                 snackbarActions.OPEN_SNACKBAR({
87                     message: successMessage,
88                     hideDuration: 2000,
89                     kind: SnackbarKind.SUCCESS,
90                 })
91             );
92         } catch (e) {
93             if (e.status === 422) {
94                 dispatch(
95                     snackbarActions.OPEN_SNACKBAR({
96                         message: "Could not restore collection from trash: Duplicate name at destination",
97                         kind: SnackbarKind.ERROR,
98                     })
99                 );
100             } else {
101                 dispatch(
102                     snackbarActions.OPEN_SNACKBAR({
103                         message: errorMessage,
104                         kind: SnackbarKind.ERROR,
105                     })
106                 );
107             }
108         }
109     };
110
111 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) => (dispatch: Dispatch) => {
112     if (kind === ResourceKind.PROJECT) {
113         dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!, false));
114     } else if (kind === ResourceKind.COLLECTION) {
115         dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));
116     }
117 };