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 { projectPanelDataActions } 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";
16 import { ContextMenuActionNames } from "views-components/context-menu/context-menu-action-set";
17 import { addDisabledButton } from "store/multiselect/multiselect-actions";
19 export const toggleProjectTrashed =
20 (uuid: string, ownerUuid: string, isTrashed: boolean, isMulti: boolean) =>
21 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
22 let errorMessage = "";
23 let successMessage = "";
24 let untrashedResource;
25 dispatch<any>(addDisabledButton(ContextMenuActionNames.MOVE_TO_TRASH))
28 errorMessage = "Could not restore project from trash";
29 successMessage = "Restored project from trash";
30 untrashedResource = await services.groupsService.untrash(uuid);
31 dispatch<any>(isMulti || !untrashedResource ? navigateToTrash : navigateTo(uuid));
32 dispatch<any>(activateSidePanelTreeItem(uuid));
34 errorMessage = "Could not move project to trash";
35 successMessage = "Added project to trash";
36 await services.groupsService.trash(uuid);
37 dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
39 const { location } = getState().router;
40 if (matchSharedWithMeRoute(location ? location.pathname : "")) {
41 dispatch(sharedWithMePanelActions.REQUEST_ITEMS());
44 dispatch<any>(navigateTo(ownerUuid));
47 if (untrashedResource) {
49 snackbarActions.OPEN_SNACKBAR({
50 message: successMessage,
52 kind: SnackbarKind.SUCCESS,
57 if (e.status === 422) {
59 snackbarActions.OPEN_SNACKBAR({
60 message: "Could not restore project from trash: Duplicate name at destination",
61 kind: SnackbarKind.ERROR,
66 snackbarActions.OPEN_SNACKBAR({
67 message: errorMessage,
68 kind: SnackbarKind.ERROR,
75 export const toggleCollectionTrashed =
76 (uuid: string, isTrashed: boolean) =>
77 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
78 let errorMessage = "";
79 let successMessage = "";
80 dispatch<any>(addDisabledButton(ContextMenuActionNames.MOVE_TO_TRASH))
83 const { location } = getState().router;
84 errorMessage = "Could not restore collection from trash";
85 successMessage = "Restored from trash";
86 await services.collectionService.untrash(uuid);
87 if (matchCollectionRoute(location ? location.pathname : "")) {
88 dispatch(navigateToTrash);
90 dispatch(trashPanelActions.REQUEST_ITEMS());
92 errorMessage = "Could not move collection to trash";
93 successMessage = "Added to trash";
94 await services.collectionService.trash(uuid);
95 dispatch(projectPanelDataActions.REQUEST_ITEMS());
98 snackbarActions.OPEN_SNACKBAR({
99 message: successMessage,
101 kind: SnackbarKind.SUCCESS,
105 if (e.status === 422) {
107 snackbarActions.OPEN_SNACKBAR({
108 message: "Could not restore collection from trash: Duplicate name at destination",
109 kind: SnackbarKind.ERROR,
114 snackbarActions.OPEN_SNACKBAR({
115 message: errorMessage,
116 kind: SnackbarKind.ERROR,
123 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) => (dispatch: Dispatch) => {
124 if (kind === ResourceKind.PROJECT) {
125 dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!, false));
126 } else if (kind === ResourceKind.COLLECTION) {
127 dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));