sharing-with-groups
[arvados.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";
12 import { ResourceKind } from "~/models/resource";
13 import { navigateToTrash } from '~/store/navigation/navigation-action';
14 import { matchCollectionRoute } from '~/routes/routes';
15
16 export const toggleProjectTrashed = (uuid: string, ownerUuid: string, isTrashed: boolean) =>
17     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
18         try {
19             if (isTrashed) {
20                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Restoring from trash...", kind: SnackbarKind.INFO }));
21                 await services.groupsService.untrash(uuid);
22                 dispatch<any>(activateSidePanelTreeItem(uuid));
23                 dispatch(trashPanelActions.REQUEST_ITEMS());
24                 dispatch(snackbarActions.OPEN_SNACKBAR({
25                     message: "Restored from trash",
26                     hideDuration: 2000,
27                     kind: SnackbarKind.SUCCESS
28                 }));
29             } else {
30                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Moving to trash...", kind: SnackbarKind.INFO }));
31                 await services.groupsService.trash(uuid);
32                 dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
33                 dispatch(snackbarActions.OPEN_SNACKBAR({
34                     message: "Added to trash",
35                     hideDuration: 2000,
36                     kind: SnackbarKind.SUCCESS
37                 }));
38             }
39         } catch (e) {
40             dispatch(snackbarActions.OPEN_SNACKBAR({
41                 message: "Could not move project to trash",
42                 kind: SnackbarKind.ERROR
43             }));
44         }
45     };
46
47 export const toggleCollectionTrashed = (uuid: string, isTrashed: boolean) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
49         try {
50             if (isTrashed) {
51                 const { location } = getState().router;
52                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Restoring from trash...", kind: SnackbarKind.INFO }));
53                 await services.collectionService.untrash(uuid);
54                 if (matchCollectionRoute(location ? location.pathname : '')) {
55                     dispatch(navigateToTrash);
56                 }
57                 dispatch(trashPanelActions.REQUEST_ITEMS());
58                 dispatch(snackbarActions.OPEN_SNACKBAR({
59                     message: "Restored from trash",
60                     hideDuration: 2000,
61                     kind: SnackbarKind.SUCCESS
62                 }));
63             } else {
64                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Moving to trash...", kind: SnackbarKind.INFO }));
65                 await services.collectionService.trash(uuid);
66                 dispatch(projectPanelActions.REQUEST_ITEMS());
67                 dispatch(snackbarActions.OPEN_SNACKBAR({
68                     message: "Added to trash",
69                     hideDuration: 2000,
70                     kind: SnackbarKind.SUCCESS
71                 }));
72             }
73         } catch (e) {
74             dispatch(snackbarActions.OPEN_SNACKBAR({
75                 message: "Could not move collection to trash",
76                 kind: SnackbarKind.ERROR
77             }));
78         }
79     };
80
81 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) =>
82     (dispatch: Dispatch) => {
83         if (kind === ResourceKind.PROJECT) {
84             dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!));
85         } else if (kind === ResourceKind.COLLECTION) {
86             dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));
87         }
88     };