17266: Fixes integration test.
[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(projectPanelActions.REQUEST_ITEMS());
33                 dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
34                 dispatch(snackbarActions.OPEN_SNACKBAR({
35                     message: "Added to trash",
36                     hideDuration: 2000,
37                     kind: SnackbarKind.SUCCESS
38                 }));
39             }
40         } catch (e) {
41             dispatch(snackbarActions.OPEN_SNACKBAR({
42                 message: "Could not move project to trash",
43                 kind: SnackbarKind.ERROR
44             }));
45         }
46     };
47
48 export const toggleCollectionTrashed = (uuid: string, isTrashed: boolean) =>
49     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
50         try {
51             if (isTrashed) {
52                 const { location } = getState().router;
53                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Restoring from trash...", kind: SnackbarKind.INFO }));
54                 await services.collectionService.untrash(uuid);
55                 if (matchCollectionRoute(location ? location.pathname : '')) {
56                     dispatch(navigateToTrash);
57                 }
58                 dispatch(trashPanelActions.REQUEST_ITEMS());
59                 dispatch(snackbarActions.OPEN_SNACKBAR({
60                     message: "Restored from trash",
61                     hideDuration: 2000,
62                     kind: SnackbarKind.SUCCESS
63                 }));
64             } else {
65                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Moving to trash...", kind: SnackbarKind.INFO }));
66                 await services.collectionService.trash(uuid);
67                 dispatch(projectPanelActions.REQUEST_ITEMS());
68                 dispatch(snackbarActions.OPEN_SNACKBAR({
69                     message: "Added to trash",
70                     hideDuration: 2000,
71                     kind: SnackbarKind.SUCCESS
72                 }));
73             }
74         } catch (e) {
75             dispatch(snackbarActions.OPEN_SNACKBAR({
76                 message: "Could not move collection to trash",
77                 kind: SnackbarKind.ERROR
78             }));
79         }
80     };
81
82 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) =>
83     (dispatch: Dispatch) => {
84         if (kind === ResourceKind.PROJECT) {
85             dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!));
86         } else if (kind === ResourceKind.COLLECTION) {
87             dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));
88         }
89     };