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 { 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';
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 = '';
22 errorMessage = "Could not restore project from trash";
23 successMessage = "Restored from trash";
24 await services.groupsService.untrash(uuid);
25 dispatch<any>(navigateTo(uuid));
26 dispatch<any>(activateSidePanelTreeItem(uuid));
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));
34 dispatch(projectPanelActions.REQUEST_ITEMS());
36 dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
39 dispatch(snackbarActions.OPEN_SNACKBAR({
40 message: errorMessage,
41 kind: SnackbarKind.ERROR
44 dispatch(snackbarActions.OPEN_SNACKBAR({
45 message: successMessage,
47 kind: SnackbarKind.SUCCESS
51 export const toggleCollectionTrashed = (uuid: string, isTrashed: boolean) =>
52 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
53 let errorMessage = '';
54 let successMessage = '';
57 const { location } = getState().router;
58 errorMessage = "Could not restore collection from trash";
59 successMessage = "Restored from trash";
60 await services.collectionService.untrash(uuid);
61 if (matchCollectionRoute(location ? location.pathname : '')) {
62 dispatch(navigateToTrash);
64 dispatch(trashPanelActions.REQUEST_ITEMS());
66 errorMessage = "Could not move collection to trash";
67 successMessage = "Added to trash";
68 await services.collectionService.trash(uuid);
69 dispatch(projectPanelActions.REQUEST_ITEMS());
72 dispatch(snackbarActions.OPEN_SNACKBAR({
73 message: errorMessage,
74 kind: SnackbarKind.ERROR
77 dispatch(snackbarActions.OPEN_SNACKBAR({
78 message: successMessage,
80 kind: SnackbarKind.SUCCESS
84 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) =>
85 (dispatch: Dispatch) => {
86 if (kind === ResourceKind.PROJECT) {
87 dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!));
88 } else if (kind === ResourceKind.COLLECTION) {
89 dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));