c6d4ee0af56c2dd30abca4ed75dc2a0d05501a59
[arvados-workbench2.git] / src / store / collections / collection-trash-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from "~/store/store";
7 import { ServiceRepository } from "~/services/services";
8 import { snackbarActions } from "~/store/snackbar/snackbar-actions";
9 import { sidePanelActions } from "~/store/side-panel/side-panel-action";
10 import { SidePanelId } from "~/store/side-panel/side-panel-reducer";
11 import { trashPanelActions } from "~/store/trash-panel/trash-panel-action";
12 import { getProjectList, projectActions } from "~/store/project/project-action";
13
14 export const toggleCollectionTrashed = (resource: { uuid: string; name: string, isTrashed?: boolean, ownerUuid?: string }) =>
15     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
16         dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Working..." }));
17         if (resource.isTrashed) {
18             return services.collectionService.untrash(resource.uuid).then(() => {
19                 dispatch<any>(getProjectList(resource.ownerUuid)).then(() => {
20                     dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelId.PROJECTS));
21                     dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN({ itemId: resource.ownerUuid!!, open: true, recursive: true }));
22                 });
23                 dispatch(trashPanelActions.REQUEST_ITEMS());
24                 dispatch(snackbarActions.CLOSE_SNACKBAR());
25                 dispatch(snackbarActions.OPEN_SNACKBAR({
26                     message: "Restored from trash",
27                     hideDuration: 2000
28                 }));
29             });
30         } else {
31             return services.collectionService.trash(resource.uuid).then(() => {
32                 dispatch(snackbarActions.CLOSE_SNACKBAR());
33                 dispatch(snackbarActions.OPEN_SNACKBAR({
34                     message: "Added to trash",
35                     hideDuration: 2000
36                 }));
37             });
38         }
39     };