15768: collection copy dialog mostly works 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 export const COLLECTION_MULTI_COPY_FORM_NAME = "collectionMultiCopyFormName";
20
21 export const openCollectionCopyDialog = (resource: { name: string; uuid: string; isSingle?: boolean }) => (dispatch: Dispatch) => {
22     //here
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, isSingle: resource.isSingle };
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; isSingle?: boolean }) => (dispatch: Dispatch) => {
31     //here
32     dispatch<any>(resetPickerProjectTree());
33     dispatch<any>(initProjectsTreePicker(COLLECTION_MULTI_COPY_FORM_NAME));
34     const initialData: CopyFormDialogData = { name: `Copy of: ${resource.name}`, ownerUuid: "", uuid: resource.uuid, isSingle: resource.isSingle };
35     dispatch<any>(initialize(COLLECTION_MULTI_COPY_FORM_NAME, initialData));
36     dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_MULTI_COPY_FORM_NAME, data: {} }));
37 };
38
39 export const copyCollection =
40     (resource: CopyFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
41         console.log("IN COPY COLLECTION", resource);
42         const formName = resource.isSingle ? COLLECTION_COPY_FORM_NAME : COLLECTION_MULTI_COPY_FORM_NAME;
43         dispatch(startSubmit(COLLECTION_COPY_FORM_NAME));
44         let collection = getResource<CollectionResource>(resource.uuid)(getState().resources);
45         try {
46             if (!collection) {
47                 collection = await services.collectionService.get(resource.uuid);
48             }
49             const collManifestText = await services.collectionService.get(resource.uuid, undefined, ["manifestText"]);
50             collection.manifestText = collManifestText.manifestText;
51             const { href, ...collectionRecord } = collection;
52             const newCollection = await services.collectionService.create({
53                 ...collectionRecord,
54                 ownerUuid: resource.ownerUuid,
55                 name: resource.name,
56             });
57             dispatch(dialogActions.CLOSE_DIALOG({ id: formName }));
58             return newCollection;
59         } catch (e) {
60             const error = getCommonResourceServiceError(e);
61             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
62                 dispatch(
63                     stopSubmit(formName, {
64                         ownerUuid: "A collection with the same name already exists in the target project.",
65                     } as FormErrors)
66                 );
67             } else {
68                 dispatch(dialogActions.CLOSE_DIALOG({ id: formName }));
69                 throw new Error("Could not copy the collection.");
70             }
71             return;
72         } finally {
73             dispatch(progressIndicatorActions.STOP_WORKING(formName));
74         }
75     };