20031: Preselect current collection in move/copy to existing collection tree picker
[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 { FileOperationLocation } from "store/tree-picker/tree-picker-actions";
16 import { updateResources } from 'store/resources/resources-actions';
17 import { navigateTo } from 'store/navigation/navigation-action';
18
19 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
20 export const COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_COPY_TO_SELECTED_DIALOG';
21 export const COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS = 'COLLECTION_PARTIAL_COPY_TO_SEPARATE_DIALOG';
22
23 export interface CollectionPartialCopyToNewCollectionFormData {
24     name: string;
25     description: string;
26     projectUuid: string;
27 }
28
29 export interface CollectionPartialCopyToExistingCollectionFormData {
30     destination: FileOperationLocation;
31 }
32
33 export interface CollectionPartialCopyToSeparateCollectionsFormData {
34     name: string;
35     projectUuid: string;
36 }
37
38 export const openCollectionPartialCopyToNewCollectionDialog = () =>
39     (dispatch: Dispatch, getState: () => RootState) => {
40         const currentCollection = getState().collectionPanel.item;
41         if (currentCollection) {
42             const initialData = {
43                 name: `Files extracted from: ${currentCollection.name}`,
44                 description: currentCollection.description,
45                 projectUuid: undefined
46             };
47             dispatch(initialize(COLLECTION_PARTIAL_COPY_FORM_NAME, initialData));
48             dispatch<any>(resetPickerProjectTree());
49             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME, data: {} }));
50         }
51     };
52
53 export const copyCollectionPartialToNewCollection = ({ name, description, projectUuid }: CollectionPartialCopyToNewCollectionFormData) =>
54     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55         const state = getState();
56         // Get current collection
57         const sourceCollection = state.collectionPanel.item;
58
59         if (sourceCollection) {
60             try {
61                 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
62                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
63
64                 // Get selected files
65                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
66                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
67
68                 // Copy files
69                 const updatedCollection = await services.collectionService.copyFiles(
70                     sourceCollection.portableDataHash,
71                     paths,
72                     {
73                         name,
74                         description,
75                         ownerUuid: projectUuid,
76                         uuid: undefined,
77                     },
78                     '/',
79                     false
80                 );
81                 dispatch(updateResources([updatedCollection]));
82                 dispatch<any>(navigateTo(updatedCollection.uuid))
83
84                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
85                 dispatch(snackbarActions.OPEN_SNACKBAR({
86                     message: 'New collection created.',
87                     hideDuration: 2000,
88                     kind: SnackbarKind.SUCCESS
89                 }));
90                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
91             } catch (e) {
92                 const error = getCommonResourceServiceError(e);
93                 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
94                     dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME, { name: 'Collection with this name already exists.' } as FormErrors));
95                 } else if (error === CommonResourceServiceError.UNKNOWN) {
96                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
97                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
98                 } else {
99                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
100                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
101                 }
102                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
103             }
104         }
105     };
106
107 export const openCollectionPartialCopyToExistingCollectionDialog = () =>
108     (dispatch: Dispatch, getState: () => RootState) => {
109         const currentCollection = getState().collectionPanel.item;
110         if (currentCollection) {
111             const initialData = {
112                 destination: {uuid: currentCollection.uuid, destinationPath: ''}
113             };
114             dispatch(initialize(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, initialData));
115             dispatch<any>(resetPickerProjectTree());
116             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, data: {} }));
117         }
118     };
119
120 export const copyCollectionPartialToExistingCollection = ({ destination }: CollectionPartialCopyToExistingCollectionFormData) =>
121     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
122         const state = getState();
123         // Get current collection
124         const sourceCollection = state.collectionPanel.item;
125
126         if (sourceCollection && destination && destination.uuid) {
127             try {
128                 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
129                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
130                 // Get selected files
131                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
132                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
133
134                 // Copy files
135                 const updatedCollection = await services.collectionService.copyFiles(sourceCollection.portableDataHash, paths, {uuid: destination.uuid}, destination.path || '/', false);
136                 dispatch(updateResources([updatedCollection]));
137
138                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
139                 dispatch(snackbarActions.OPEN_SNACKBAR({
140                     message: 'Files has been copied to selected collection.',
141                     hideDuration: 2000,
142                     kind: SnackbarKind.SUCCESS
143                 }));
144                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
145             } catch (e) {
146                 const error = getCommonResourceServiceError(e);
147                 if (error === CommonResourceServiceError.UNKNOWN) {
148                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
149                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
150                 }
151                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
152             }
153         }
154     };
155
156 export const openCollectionPartialCopyToSeparateCollectionsDialog = () =>
157     (dispatch: Dispatch, getState: () => RootState) => {
158         const currentCollection = getState().collectionPanel.item;
159         if (currentCollection) {
160             const initialData = {
161                 name: currentCollection.name,
162                 projectUuid: undefined
163             };
164             dispatch(initialize(COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS, initialData));
165             dispatch<any>(resetPickerProjectTree());
166             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS, data: {} }));
167         }
168     };
169
170 export const copyCollectionPartialToSeparateCollections = ({ name, projectUuid }: CollectionPartialCopyToSeparateCollectionsFormData) =>
171     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
172         const state = getState();
173         // Get current collection
174         const sourceCollection = state.collectionPanel.item;
175
176         if (sourceCollection) {
177             try {
178                 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
179                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
180
181                 // Get selected files
182                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
183                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
184
185                 // Copy files
186                 const collections = await Promise.all(paths.map((path) =>
187                     services.collectionService.copyFiles(
188                         sourceCollection.portableDataHash,
189                         [path],
190                         {
191                             name: `File split from collection ${name}${path}`,
192                             ownerUuid: projectUuid,
193                             uuid: undefined,
194                         },
195                         '/',
196                         false
197                     )
198                 ));
199                 dispatch(updateResources(collections));
200
201                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
202                 dispatch(snackbarActions.OPEN_SNACKBAR({
203                     message: 'New collections created.',
204                     hideDuration: 2000,
205                     kind: SnackbarKind.SUCCESS
206                 }));
207                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
208             } catch (e) {
209                 const error = getCommonResourceServiceError(e);
210                 console.log(e, error);
211                 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
212                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection from one or more files already exists', hideDuration: 2000, kind: SnackbarKind.ERROR }));
213                 } else if (error === CommonResourceServiceError.UNKNOWN) {
214                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
215                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
216                 } else {
217                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
218                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
219                 }
220                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
221             }
222         }
223     };