Add pickerId prop to DialogCopy
[arvados.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 { 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
15 export const COLLECTION_COPY_FORM_NAME = 'collectionCopyFormName';
16
17 export const openCollectionCopyDialog = (resource: { name: string, uuid: string }) =>
18     (dispatch: Dispatch) => {
19         dispatch<any>(resetPickerProjectTree());
20         const initialData: CopyFormDialogData = { name: `Copy of: ${resource.name}`, ownerUuid: '', uuid: resource.uuid };
21         dispatch<any>(initialize(COLLECTION_COPY_FORM_NAME, initialData));
22         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_COPY_FORM_NAME, data: {} }));
23     };
24
25 export const copyCollection = (resource: CopyFormDialogData) =>
26     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
27         dispatch(startSubmit(COLLECTION_COPY_FORM_NAME));
28         try {
29             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_COPY_FORM_NAME));
30             const collection = await services.collectionService.get(resource.uuid);
31             const uuidKey = 'uuid';
32             delete collection[uuidKey];
33             const newCollection = await services.collectionService.create({ ...collection, ownerUuid: resource.ownerUuid, name: resource.name });
34             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
35             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_COPY_FORM_NAME));
36             return newCollection;
37         } catch (e) {
38             const error = getCommonResourceServiceError(e);
39             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
40                 dispatch(stopSubmit(COLLECTION_COPY_FORM_NAME, { ownerUuid: 'A collection with the same name already exists in the target project.' }));
41             } else {
42                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
43                 throw new Error('Could not copy the collection.');
44             }
45             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_COPY_FORM_NAME));
46             return;
47         }
48     };