20031: Add ability to create and update collections during replace_files, moveFiles...
[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 { 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";
16 import { updateResources } from 'store/resources/resources-actions';
17
18 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
19 export const COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_COPY_TO_SELECTED_DIALOG';
20
21 export interface CollectionPartialCopyToNewCollectionFormData {
22     name: string;
23     description: string;
24     projectUuid: string;
25 }
26
27 export interface CollectionPartialCopyToExistingCollectionFormData {
28     collectionUuid: string;
29 }
30
31 export const openCollectionPartialCopyToNewCollectionDialog = () =>
32     (dispatch: Dispatch, getState: () => RootState) => {
33         const currentCollection = getState().collectionPanel.item;
34         if (currentCollection) {
35             const initialData = {
36                 name: `Files extracted from: ${currentCollection.name}`,
37                 description: currentCollection.description,
38                 projectUuid: undefined
39             };
40             dispatch(initialize(COLLECTION_PARTIAL_COPY_FORM_NAME, initialData));
41             dispatch<any>(resetPickerProjectTree());
42             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_COPY_FORM_NAME));
43             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME, data: {} }));
44         }
45     };
46
47 export const copyCollectionPartialToNewCollection = ({ name, description, projectUuid }: CollectionPartialCopyToNewCollectionFormData) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49         const state = getState();
50         // Get current collection
51         const sourceCollection = state.collectionPanel.item;
52
53         if (sourceCollection) {
54             try {
55                 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
56                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
57
58                 // Get selected files
59                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
60                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
61
62                 // Copy files
63                 const updatedCollection = await services.collectionService.copyFiles(
64                     sourceCollection.portableDataHash,
65                     paths,
66                     {
67                         name,
68                         description,
69                         ownerUuid: projectUuid,
70                         uuid: undefined,
71                     },
72                     '/',
73                     false
74                 );
75                 dispatch(updateResources([updatedCollection]));
76
77                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
78                 dispatch(snackbarActions.OPEN_SNACKBAR({
79                     message: 'New collection created.',
80                     hideDuration: 2000,
81                     kind: SnackbarKind.SUCCESS
82                 }));
83                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
84             } catch (e) {
85                 const error = getCommonResourceServiceError(e);
86                 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
87                     dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME, { name: 'Collection with this name already exists.' } as FormErrors));
88                 } else if (error === CommonResourceServiceError.UNKNOWN) {
89                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
90                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
91                 } else {
92                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
93                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
94                 }
95                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
96             }
97         }
98     };
99
100 export const openCollectionPartialCopyToExistingCollectionDialog = () =>
101     (dispatch: Dispatch, getState: () => RootState) => {
102         const currentCollection = getState().collectionPanel.item;
103         if (currentCollection) {
104             const initialData = {
105                 collectionUuid: ''
106             };
107             dispatch(initialize(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, initialData));
108             dispatch<any>(resetPickerProjectTree());
109             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
110             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, data: {} }));
111         }
112     };
113
114 export const copyCollectionPartialToExistingCollection = ({ collectionUuid }: CollectionPartialCopyToExistingCollectionFormData) =>
115     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
116         const state = getState();
117         // Get current collection
118         const sourceCollection = state.collectionPanel.item;
119
120         if (sourceCollection) {
121             try {
122                 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
123                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
124                 // Get selected files
125                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
126                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
127
128                 // Copy files
129                 const updatedCollection = await services.collectionService.copyFiles(sourceCollection.portableDataHash, paths, {uuid: collectionUuid}, '/', false);
130                 dispatch(updateResources([updatedCollection]));
131
132                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
133                 dispatch(snackbarActions.OPEN_SNACKBAR({
134                     message: 'Files has been copied to selected collection.',
135                     hideDuration: 2000,
136                     kind: SnackbarKind.SUCCESS
137                 }));
138                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
139             } catch (e) {
140                 const error = getCommonResourceServiceError(e);
141                 if (error === CommonResourceServiceError.UNKNOWN) {
142                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
143                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
144                 }
145                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
146             }
147         }
148     };