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 { 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 '~/common/api/common-resource-service';
13 export const COLLECTION_COPY_FORM_NAME = 'collectionCopyFormName';
15 export interface CollectionCopyFormDialogData {
21 export const openCollectionCopyDialog = (resource: { name: string, uuid: string }) =>
22 (dispatch: Dispatch) => {
23 dispatch<any>(resetPickerProjectTree());
24 const initialData: CollectionCopyFormDialogData = { 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: CollectionCopyFormDialogData) =>
30 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
31 dispatch(startSubmit(COLLECTION_COPY_FORM_NAME));
33 const collection = await services.collectionService.get(resource.uuid);
34 const uuidKey = 'uuid';
35 delete collection[uuidKey];
36 await services.collectionService.create({ ...collection, ownerUuid: resource.ownerUuid, name: resource.name });
37 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
40 const error = getCommonResourceServiceError(e);
41 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
42 dispatch(stopSubmit(COLLECTION_COPY_FORM_NAME, { ownerUuid: 'A collection with the same name already exists in the target project.' }));
44 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
45 throw new Error('Could not copy the collection.');