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, SnackbarKind } from '~/store/snackbar/snackbar-actions';
13 import { getCommonResourceServiceError, CommonResourceServiceError } from '~/services/common-service/common-resource-service';
14 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
16 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
18 export interface CollectionPartialCopyFormData {
24 export const openCollectionPartialCopyDialog = () =>
25 (dispatch: Dispatch, getState: () => RootState) => {
26 const currentCollection = getState().collectionPanel.item;
27 if (currentCollection) {
29 name: currentCollection.name,
30 description: currentCollection.description,
33 dispatch(initialize(COLLECTION_PARTIAL_COPY_FORM_NAME, initialData));
34 dispatch<any>(resetPickerProjectTree());
35 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME, data: {} }));
39 export const copyCollectionPartial = ({ name, description, projectUuid }: CollectionPartialCopyFormData) =>
40 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
41 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
42 const state = getState();
43 const currentCollection = state.collectionPanel.item;
44 if (currentCollection) {
46 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
47 const collection = await services.collectionService.get(currentCollection.uuid);
48 const collectionCopy = {
52 ownerUuid: projectUuid,
55 const newCollection = await services.collectionService.create(collectionCopy);
56 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, false).map(file => file.id);
57 await services.collectionService.deleteFiles(newCollection.uuid, paths);
58 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
59 dispatch(snackbarActions.OPEN_SNACKBAR({
60 message: 'New collection created.',
62 kind: SnackbarKind.SUCCESS
64 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
66 const error = getCommonResourceServiceError(e);
67 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
68 dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME, { name: 'Collection with this name already exists.' }));
69 } else if (error === CommonResourceServiceError.UNKNOWN) {
70 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
71 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000 }));
73 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
74 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000 }));
76 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));