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