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