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 { ResourceKind } from "models/resource";
13 import { navigateTo, navigateToTrash } from "store/navigation/navigation-action";
14 import { matchCollectionRoute } from "routes/routes";
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 let untrashedResource;
24 errorMessage = "Could not restore project from trash";
25 successMessage = "Restored project from trash";
26 untrashedResource = await services.groupsService.untrash(uuid);
27 dispatch<any>(isMulti || !untrashedResource ? navigateToTrash : navigateTo(uuid));
28 dispatch<any>(activateSidePanelTreeItem(uuid));
30 errorMessage = "Could not move project to trash";
31 successMessage = "Added project to trash";
32 await services.groupsService.trash(uuid);
33 dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
34 dispatch<any>(navigateTo(ownerUuid));
36 if (untrashedResource) {
38 snackbarActions.OPEN_SNACKBAR({
39 message: successMessage,
41 kind: SnackbarKind.SUCCESS,
46 if (e.status === 422) {
48 snackbarActions.OPEN_SNACKBAR({
49 message: "Could not restore project from trash: Duplicate name at destination",
50 kind: SnackbarKind.ERROR,
55 snackbarActions.OPEN_SNACKBAR({
56 message: errorMessage,
57 kind: SnackbarKind.ERROR,
64 export const toggleCollectionTrashed =
65 (uuid: string, isTrashed: boolean) =>
66 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
67 let errorMessage = "";
68 let successMessage = "";
71 const { location } = getState().router;
72 errorMessage = "Could not restore collection from trash";
73 successMessage = "Restored from trash";
74 await services.collectionService.untrash(uuid);
75 if (matchCollectionRoute(location ? location.pathname : "")) {
76 dispatch(navigateToTrash);
78 dispatch(trashPanelActions.REQUEST_ITEMS());
80 errorMessage = "Could not move collection to trash";
81 successMessage = "Added to trash";
82 await services.collectionService.trash(uuid);
83 dispatch(projectPanelActions.REQUEST_ITEMS());
86 snackbarActions.OPEN_SNACKBAR({
87 message: successMessage,
89 kind: SnackbarKind.SUCCESS,
93 if (e.status === 422) {
95 snackbarActions.OPEN_SNACKBAR({
96 message: "Could not restore collection from trash: Duplicate name at destination",
97 kind: SnackbarKind.ERROR,
102 snackbarActions.OPEN_SNACKBAR({
103 message: errorMessage,
104 kind: SnackbarKind.ERROR,
111 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) => (dispatch: Dispatch) => {
112 if (kind === ResourceKind.PROJECT) {
113 dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!, false));
114 } else if (kind === ResourceKind.COLLECTION) {
115 dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));