15768: collection copy works except dialog Arvados-DCO-1.1-Signed-off-by: Lisa Knox...
[arvados-workbench2.git] / 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
18 export const COLLECTION_COPY_FORM_NAME = "collectionCopyFormName";
19
20 export const openCollectionCopyDialog = (resource: { name: string; uuid: string; isSingle?: boolean }) => (dispatch: Dispatch) => {
21     dispatch<any>(resetPickerProjectTree());
22     dispatch<any>(initProjectsTreePicker(COLLECTION_COPY_FORM_NAME));
23     const initialData: CopyFormDialogData = { name: `Copy of: ${resource.name}`, ownerUuid: "", uuid: resource.uuid, isSingle: resource.isSingle };
24     dispatch<any>(initialize(COLLECTION_COPY_FORM_NAME, initialData));
25     dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_COPY_FORM_NAME, data: {} }));
26 };
27
28 export const copyCollection =
29     (resource: CopyFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
30         dispatch(startSubmit(COLLECTION_COPY_FORM_NAME));
31         let collection = getResource<CollectionResource>(resource.uuid)(getState().resources);
32         try {
33             if (!collection) {
34                 collection = await services.collectionService.get(resource.uuid);
35             }
36             const collManifestText = await services.collectionService.get(resource.uuid, undefined, ["manifestText"]);
37             collection.manifestText = collManifestText.manifestText;
38             const { href, ...collectionRecord } = collection;
39             const newCollection = await services.collectionService.create({
40                 ...collectionRecord,
41                 ownerUuid: resource.ownerUuid,
42                 name: resource.name,
43             });
44             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
45             return newCollection;
46         } catch (e) {
47             const error = getCommonResourceServiceError(e);
48             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
49                 dispatch(
50                     stopSubmit(COLLECTION_COPY_FORM_NAME, {
51                         ownerUuid: "A collection with the same name already exists in the target project.",
52                     } as FormErrors)
53                 );
54             } else {
55                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
56                 throw new Error("Could not copy the collection.");
57             }
58             return;
59         } finally {
60             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_COPY_FORM_NAME));
61         }
62     };