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