Merge branch '21448-menu-reorder' into 21224-project-details
[arvados.git] / services / workbench2 / src / store / collections / collection-copy-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 { dialogActions } from "store/dialog/dialog-actions";
7 import { FormErrors, initialize, startSubmit, stopSubmit } from "redux-form";
8 import { resetPickerProjectTree } from "store/project-tree-picker/project-tree-picker-actions";
9 import { RootState } from "store/store";
10 import { ServiceRepository } from "services/services";
11 import { getCommonResourceServiceError, CommonResourceServiceError } from "services/common-service/common-resource-service";
12 import { CopyFormDialogData } from "store/copy-dialog/copy-dialog";
13 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
14 import { initProjectsTreePicker } from "store/tree-picker/tree-picker-actions";
15 import { getResource } from "store/resources/resources";
16 import { CollectionResource } from "models/collection";
17 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
18
19 export const COLLECTION_COPY_FORM_NAME = "collectionCopyFormName";
20 export const COLLECTION_MULTI_COPY_FORM_NAME = "collectionMultiCopyFormName";
21
22 export const openCollectionCopyDialog = (resource: { name: string; uuid: string; fromContextMenu?: boolean }) => (dispatch: Dispatch) => {
23     dispatch<any>(resetPickerProjectTree());
24     dispatch<any>(initProjectsTreePicker(COLLECTION_COPY_FORM_NAME));
25     const initialData: CopyFormDialogData = { name: `Copy of: ${resource.name}`, ownerUuid: "", uuid: resource.uuid, fromContextMenu: resource.fromContextMenu };
26     dispatch<any>(initialize(COLLECTION_COPY_FORM_NAME, initialData));
27     dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_COPY_FORM_NAME, data: {} }));
28 };
29
30 export const openMultiCollectionCopyDialog = (resource: { name: string; uuid: string; fromContextMenu?: boolean }) => (dispatch: Dispatch) => {
31     dispatch<any>(resetPickerProjectTree());
32     dispatch<any>(initProjectsTreePicker(COLLECTION_MULTI_COPY_FORM_NAME));
33     const initialData: CopyFormDialogData = { name: `Copy of: ${resource.name}`, ownerUuid: "", uuid: resource.uuid, fromContextMenu: resource.fromContextMenu };
34     dispatch<any>(initialize(COLLECTION_MULTI_COPY_FORM_NAME, initialData));
35     dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_MULTI_COPY_FORM_NAME, data: {} }));
36 };
37
38 export const copyCollection =
39     (resource: CopyFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40         const formName = resource.fromContextMenu ? COLLECTION_COPY_FORM_NAME : COLLECTION_MULTI_COPY_FORM_NAME;
41         dispatch(startSubmit(formName));
42         let collection = getResource<CollectionResource>(resource.uuid)(getState().resources);
43         try {
44             if (!collection) {
45                 collection = await services.collectionService.get(resource.uuid);
46             }
47             const collManifestText = await services.collectionService.get(resource.uuid, undefined, ["manifestText"]);
48             collection.manifestText = collManifestText.manifestText;
49             const { href, ...collectionRecord } = collection;
50             const newCollection = await services.collectionService.create(
51                 {
52                     ...collectionRecord,
53                     ownerUuid: resource.ownerUuid,
54                     name: resource.name,
55                 },
56                 false
57             );
58             dispatch(dialogActions.CLOSE_DIALOG({ id: formName }));
59             return newCollection;
60         } catch (e) {
61             console.error("Error while copying collection: ", e);
62             const error = getCommonResourceServiceError(e);
63             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
64                 dispatch(
65                     stopSubmit(formName, {
66                         ownerUuid: "A collection with the same name already exists in the target project.",
67                     } as FormErrors)
68                 );
69                 dispatch(
70                     snackbarActions.OPEN_SNACKBAR({
71                         message: "Could not copy the collection.",
72                         hideDuration: 2000,
73                         kind: SnackbarKind.ERROR,
74                     })
75                 );
76             } else {
77                 dispatch(dialogActions.CLOSE_DIALOG({ id: formName }));
78                 throw new Error("Could not copy the collection.");
79             }
80             return;
81         } finally {
82             dispatch(progressIndicatorActions.STOP_WORKING(formName));
83         }
84     };