1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from 'redux';
6 import { RootState } from '~/store/store';
7 import { initialize, startSubmit, stopSubmit } from 'redux-form';
8 import { resetPickerProjectTree } from '~/store/project-tree-picker/project-tree-picker-actions';
9 import { dialogActions } from '~/store/dialog/dialog-actions';
10 import { ServiceRepository } from '~/services/services';
11 import { filterCollectionFilesBySelection } from '../collection-panel/collection-panel-files/collection-panel-files-state';
12 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
13 import { getCommonResourceServiceError, CommonResourceServiceError } from '~/common/api/common-resource-service';
15 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
17 export interface CollectionPartialCopyFormData {
23 export const openCollectionPartialCopyDialog = () =>
24 (dispatch: Dispatch, getState: () => RootState) => {
25 const currentCollection = getState().collectionPanel.item;
26 if (currentCollection) {
28 name: currentCollection.name,
29 description: currentCollection.description,
32 dispatch(initialize(COLLECTION_PARTIAL_COPY_FORM_NAME, initialData));
33 dispatch<any>(resetPickerProjectTree());
34 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME, data: {} }));
38 export const doCollectionPartialCopy = ({ name, description, projectUuid }: CollectionPartialCopyFormData) =>
39 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
41 const state = getState();
42 const currentCollection = state.collectionPanel.item;
43 if (currentCollection) {
45 const collection = await services.collectionService.get(currentCollection.uuid);
46 const collectionCopy = {
50 ownerUuid: projectUuid,
53 const newCollection = await services.collectionService.create(collectionCopy);
54 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, false).map(file => file.id);
55 await services.collectionService.deleteFiles(newCollection.uuid, paths);
56 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
57 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'New collection created.', hideDuration: 2000 }));
59 const error = getCommonResourceServiceError(e);
60 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
61 dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME, { name: 'Collection with this name already exists.' }));
62 } else if (error === CommonResourceServiceError.UNKNOWN) {
63 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
64 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000 }));
66 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
67 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000 }));