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";
16 import { MultiSelectMenuActionNames } from "views-components/multiselect-toolbar/ms-menu-actions";
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(MultiSelectMenuActionNames.ADD_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 = "";
81 dispatch<any>(addDisabledButton(MultiSelectMenuActionNames.ADD_TO_TRASH))
84 const { location } = getState().router;
85 errorMessage = "Could not restore collection from trash";
86 successMessage = "Restored from trash";
87 await services.collectionService.untrash(uuid);
88 if (matchCollectionRoute(location ? location.pathname : "")) {
89 dispatch(navigateToTrash);
91 dispatch(trashPanelActions.REQUEST_ITEMS());
93 errorMessage = "Could not move collection to trash";
94 successMessage = "Added to trash";
95 await services.collectionService.trash(uuid);
96 dispatch(projectPanelActions.REQUEST_ITEMS());
99 snackbarActions.OPEN_SNACKBAR({
100 message: successMessage,
102 kind: SnackbarKind.SUCCESS,
106 if (e.status === 422) {
108 snackbarActions.OPEN_SNACKBAR({
109 message: "Could not restore collection from trash: Duplicate name at destination",
110 kind: SnackbarKind.ERROR,
115 snackbarActions.OPEN_SNACKBAR({
116 message: errorMessage,
117 kind: SnackbarKind.ERROR,
124 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) => (dispatch: Dispatch) => {
125 if (kind === ResourceKind.PROJECT) {
126 dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!, false));
127 } else if (kind === ResourceKind.COLLECTION) {
128 dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));