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 { FormErrors, 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";
15 import { initProjectsTreePicker } from '~/store/tree-picker/tree-picker-actions';
17 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
19 export interface CollectionPartialCopyFormData {
25 export const openCollectionPartialCopyDialog = () =>
26 (dispatch: Dispatch, getState: () => RootState) => {
27 const currentCollection = getState().collectionPanel.item;
28 if (currentCollection) {
30 name: currentCollection.name,
31 description: currentCollection.description,
34 dispatch(initialize(COLLECTION_PARTIAL_COPY_FORM_NAME, initialData));
35 dispatch<any>(resetPickerProjectTree());
36 dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_COPY_FORM_NAME));
37 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME, data: {} }));
41 export const copyCollectionPartial = ({ name, description, projectUuid }: CollectionPartialCopyFormData) =>
42 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
44 const state = getState();
45 const currentCollection = state.collectionPanel.item;
46 if (currentCollection) {
48 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
49 const collection = await services.collectionService.get(currentCollection.uuid);
50 const collectionCopy = {
54 ownerUuid: projectUuid,
57 const newCollection = await services.collectionService.create(collectionCopy);
58 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, false).map(file => file.id);
59 await services.collectionService.deleteFiles(newCollection.uuid, paths);
60 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
61 dispatch(snackbarActions.OPEN_SNACKBAR({
62 message: 'New collection created.',
64 kind: SnackbarKind.SUCCESS
66 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
68 const error = getCommonResourceServiceError(e);
69 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
70 dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME, { name: 'Collection with this name already exists.' } as FormErrors));
71 } else if (error === CommonResourceServiceError.UNKNOWN) {
72 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
73 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
75 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
76 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
78 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));