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