Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 14433_properties_i...
[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 import { initProjectsTreePicker } from '~/store/tree-picker/tree-picker-actions';
16
17 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
18
19 export interface CollectionPartialCopyFormData {
20     name: string;
21     description: string;
22     projectUuid: string;
23 }
24
25 export const openCollectionPartialCopyDialog = () =>
26     (dispatch: Dispatch, getState: () => RootState) => {
27         const currentCollection = getState().collectionPanel.item;
28         if (currentCollection) {
29             const initialData = {
30                 name: currentCollection.name,
31                 description: currentCollection.description,
32                 projectUuid: ''
33             };
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: {} }));
38         }
39     };
40
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) {
47             try {
48                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
49                 const collection = await services.collectionService.get(currentCollection.uuid);
50                 const collectionCopy = {
51                     ...collection,
52                     name,
53                     description,
54                     ownerUuid: projectUuid,
55                     uuid: undefined
56                 };
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.',
63                     hideDuration: 2000,
64                     kind: SnackbarKind.SUCCESS
65                 }));
66                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
67             } catch (e) {
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.' }));
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 }));
74                 } else {
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 }));
77                 }
78                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
79             }
80         }
81     };