21128: basic collection actioons good Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[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.ADD_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             console.log('hi')
81             dispatch<any>(addDisabledButton(MultiSelectMenuActionNames.ADD_TO_TRASH))
82             try {
83                 if (isTrashed) {
84                     const { location } = getState().router;
85                     errorMessage = "Could not restore collection from trash";
86                     successMessage = "Restored from trash";
87                     await services.collectionService.untrash(uuid);
88                     if (matchCollectionRoute(location ? location.pathname : "")) {
89                         dispatch(navigateToTrash);
90                     }
91                     dispatch(trashPanelActions.REQUEST_ITEMS());
92                 } else {
93                     errorMessage = "Could not move collection to trash";
94                     successMessage = "Added to trash";
95                     await services.collectionService.trash(uuid);
96                     dispatch(projectPanelActions.REQUEST_ITEMS());
97                 }
98                 dispatch(
99                     snackbarActions.OPEN_SNACKBAR({
100                         message: successMessage,
101                         hideDuration: 2000,
102                         kind: SnackbarKind.SUCCESS,
103                     })
104                 );
105             } catch (e) {
106                 if (e.status === 422) {
107                     dispatch(
108                         snackbarActions.OPEN_SNACKBAR({
109                             message: "Could not restore collection from trash: Duplicate name at destination",
110                             kind: SnackbarKind.ERROR,
111                         })
112                     );
113                 } else {
114                     dispatch(
115                         snackbarActions.OPEN_SNACKBAR({
116                             message: errorMessage,
117                             kind: SnackbarKind.ERROR,
118                         })
119                     );
120                 }
121             }
122         };
123
124 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) => (dispatch: Dispatch) => {
125     if (kind === ResourceKind.PROJECT) {
126         dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!, false));
127     } else if (kind === ResourceKind.COLLECTION) {
128         dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));
129     }
130 };