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