4dac9c7d7e5ce55d4246c885dcb7a707b54e7957
[arvados-workbench2.git] / src / store / collections / collection-partial-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 { 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";
15
16 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
17
18 export interface CollectionPartialCopyFormData {
19     name: string;
20     description: string;
21     projectUuid: string;
22 }
23
24 export const openCollectionPartialCopyDialog = () =>
25     (dispatch: Dispatch, getState: () => RootState) => {
26         const currentCollection = getState().collectionPanel.item;
27         if (currentCollection) {
28             const initialData = {
29                 name: currentCollection.name,
30                 description: currentCollection.description,
31                 projectUuid: ''
32             };
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: {} }));
36         }
37     };
38
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) {
45             try {
46                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
47                 const collection = await services.collectionService.get(currentCollection.uuid);
48                 const collectionCopy = {
49                     ...collection,
50                     name,
51                     description,
52                     ownerUuid: projectUuid,
53                     uuid: undefined
54                 };
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.',
61                     hideDuration: 2000,
62                     kind: SnackbarKind.SUCCESS
63                 }));
64                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
65             } catch (e) {
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 }));
72                 } else {
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 }));
75                 }
76                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
77             }
78         }
79     };