1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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-bind";
12 import { sharedWithMePanelActions } from "store/shared-with-me-panel/shared-with-me-panel-actions";
13 import { ResourceKind } from "models/resource";
14 import { navigateTo, navigateToTrash } from "store/navigation/navigation-action";
15 import { matchCollectionRoute, matchSharedWithMeRoute } from "routes/routes";
17 export const toggleProjectTrashed =
18 (uuid: string, ownerUuid: string, isTrashed: boolean, isMulti: boolean) =>
19 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
20 let errorMessage = "";
21 let successMessage = "";
22 let untrashedResource;
25 errorMessage = "Could not restore project from trash";
26 successMessage = "Restored project from trash";
27 untrashedResource = await services.groupsService.untrash(uuid);
28 dispatch<any>(isMulti || !untrashedResource ? navigateToTrash : navigateTo(uuid));
29 dispatch<any>(activateSidePanelTreeItem(uuid));
31 errorMessage = "Could not move project to trash";
32 successMessage = "Added project to trash";
33 await services.groupsService.trash(uuid);
34 dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
36 const { location } = getState().router;
37 if (matchSharedWithMeRoute(location ? location.pathname : "")) {
38 dispatch(sharedWithMePanelActions.REQUEST_ITEMS());
41 dispatch<any>(navigateTo(ownerUuid));
44 if (untrashedResource) {
46 snackbarActions.OPEN_SNACKBAR({
47 message: successMessage,
49 kind: SnackbarKind.SUCCESS,
54 if (e.status === 422) {
56 snackbarActions.OPEN_SNACKBAR({
57 message: "Could not restore project from trash: Duplicate name at destination",
58 kind: SnackbarKind.ERROR,
63 snackbarActions.OPEN_SNACKBAR({
64 message: errorMessage,
65 kind: SnackbarKind.ERROR,
72 export const toggleCollectionTrashed =
73 (uuid: string, isTrashed: boolean) =>
74 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
75 let errorMessage = "";
76 let successMessage = "";
79 const { location } = getState().router;
80 errorMessage = "Could not restore collection from trash";
81 successMessage = "Restored from trash";
82 await services.collectionService.untrash(uuid);
83 if (matchCollectionRoute(location ? location.pathname : "")) {
84 dispatch(navigateToTrash);
86 dispatch(trashPanelActions.REQUEST_ITEMS());
88 errorMessage = "Could not move collection to trash";
89 successMessage = "Added to trash";
90 await services.collectionService.trash(uuid);
91 dispatch(projectPanelActions.REQUEST_ITEMS());
94 snackbarActions.OPEN_SNACKBAR({
95 message: successMessage,
97 kind: SnackbarKind.SUCCESS,
101 if (e.status === 422) {
103 snackbarActions.OPEN_SNACKBAR({
104 message: "Could not restore collection from trash: Duplicate name at destination",
105 kind: SnackbarKind.ERROR,
110 snackbarActions.OPEN_SNACKBAR({
111 message: errorMessage,
112 kind: SnackbarKind.ERROR,
119 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) => (dispatch: Dispatch) => {
120 if (kind === ResourceKind.PROJECT) {
121 dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!, false));
122 } else if (kind === ResourceKind.COLLECTION) {
123 dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));