1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
18 export const COLLECTION_COPY_FORM_NAME = 'collectionCopyFormName';
20 export const openCollectionCopyDialog = (resource: { name: string, uuid: string }) =>
21 (dispatch: Dispatch) => {
22 dispatch<any>(resetPickerProjectTree());
23 dispatch<any>(initProjectsTreePicker(COLLECTION_COPY_FORM_NAME));
24 const initialData: CopyFormDialogData = { name: `Copy of: ${resource.name}`, ownerUuid: '', uuid: resource.uuid };
25 dispatch<any>(initialize(COLLECTION_COPY_FORM_NAME, initialData));
26 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_COPY_FORM_NAME, data: {} }));
29 export const copyCollection = (resource: CopyFormDialogData) =>
30 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
31 dispatch(startSubmit(COLLECTION_COPY_FORM_NAME));
32 let collection = getResource<CollectionResource>(resource.uuid)(getState().resources);
35 collection = await services.collectionService.get(resource.uuid);
37 const collManifestText = await services.collectionService.get(resource.uuid, undefined, ['manifestText']);
38 collection.manifestText = collManifestText.manifestText;
39 const {href, ...collectionRecord} = collection;
40 const newCollection = await services.collectionService.create({ ...collectionRecord, ownerUuid: resource.ownerUuid, name: resource.name });
41 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
44 const error = getCommonResourceServiceError(e);
45 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
47 COLLECTION_COPY_FORM_NAME,
48 { ownerUuid: 'A collection with the same name already exists in the target project.' } as FormErrors
51 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
52 throw new Error('Could not copy the collection.');
56 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_COPY_FORM_NAME));