20031: Cancel submit spinner on collection file move/copy error
[arvados-workbench2.git] / src / store / collections / collection-partial-move-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 { initialize, startSubmit, stopSubmit } from "redux-form";
7 import { CommonResourceServiceError, getCommonResourceServiceError } from "services/common-service/common-resource-service";
8 import { ServiceRepository } from "services/services";
9 import { CollectionFileSelection, CollectionPanelDirectory, CollectionPanelFile, filterCollectionFilesBySelection, getCollectionSelection } from "store/collection-panel/collection-panel-files/collection-panel-files-state";
10 import { ContextMenuResource } from "store/context-menu/context-menu-actions";
11 import { dialogActions } from "store/dialog/dialog-actions";
12 import { navigateTo } from "store/navigation/navigation-action";
13 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
14 import { resetPickerProjectTree } from "store/project-tree-picker/project-tree-picker-actions";
15 import { updateResources } from "store/resources/resources-actions";
16 import { SnackbarKind, snackbarActions } from "store/snackbar/snackbar-actions";
17 import { RootState } from "store/store";
18 import { FileOperationLocation } from "store/tree-picker/tree-picker-actions";
19 import { CollectionResource } from "models/collection";
20
21 export const COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_NEW_DIALOG';
22 export const COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_SELECTED_DIALOG';
23 export const COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS = 'COLLECTION_PARTIAL_MOVE_TO_SEPARATE_DIALOG';
24
25 export interface CollectionPartialMoveToNewCollectionFormData {
26     name: string;
27     description: string;
28     projectUuid: string;
29 }
30
31 export interface CollectionPartialMoveToExistingCollectionFormData {
32     destination: FileOperationLocation;
33 }
34
35 export interface CollectionPartialMoveToSeparateCollectionsFormData {
36     name: string;
37     projectUuid: string;
38 }
39
40 export const openCollectionPartialMoveToNewCollectionDialog = (resource: ContextMenuResource) =>
41     (dispatch: Dispatch, getState: () => RootState) => {
42         const sourceCollection = getState().collectionPanel.item;
43
44         if (sourceCollection) {
45             openMoveToNewDialog(dispatch, sourceCollection, [resource]);
46         }
47     };
48
49 export const openCollectionPartialMoveMultipleToNewCollectionDialog = () =>
50     (dispatch: Dispatch, getState: () => RootState) => {
51         const sourceCollection = getState().collectionPanel.item;
52         const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
53
54         if (sourceCollection && selectedItems.length) {
55             openMoveToNewDialog(dispatch, sourceCollection, selectedItems);
56         }
57     };
58
59 const openMoveToNewDialog = (dispatch: Dispatch, sourceCollection: CollectionResource, selectedItems: (CollectionPanelDirectory | CollectionPanelFile | ContextMenuResource)[]) => {
60     // Get selected files
61     const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
62     // Populate form initial state
63     const initialFormData = {
64         name: `Files moved from: ${sourceCollection.name}`,
65         description: sourceCollection.description,
66         projectUuid: undefined
67     };
68     dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, initialFormData));
69     dispatch<any>(resetPickerProjectTree());
70     dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, data: collectionFileSelection }));
71 }
72
73 export const moveCollectionPartialToNewCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToNewCollectionFormData) =>
74     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75         if (fileSelection.collection) {
76             try {
77                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
78                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
79
80                 // Move files
81                 const updatedCollection = await services.collectionService.moveFiles(
82                     fileSelection.collection.uuid,
83                     fileSelection.collection.portableDataHash,
84                     fileSelection.selectedPaths,
85                     {
86                         name: formData.name,
87                         description: formData.description,
88                         ownerUuid: formData.projectUuid,
89                         uuid: undefined,
90                     },
91                     '/',
92                     false
93                 );
94                 dispatch(updateResources([updatedCollection]));
95                 dispatch<any>(navigateTo(updatedCollection.uuid));
96
97                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
98                 dispatch(snackbarActions.OPEN_SNACKBAR({
99                     message: 'Files have been moved to selected collection.',
100                     hideDuration: 2000,
101                     kind: SnackbarKind.SUCCESS
102                 }));
103                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
104             } catch (e) {
105                 const error = getCommonResourceServiceError(e);
106                 if (error === CommonResourceServiceError.UNKNOWN) {
107                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
108                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
109                 }
110                 dispatch(stopSubmit(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
111                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
112             }
113         }
114     };
115
116 export const openCollectionPartialMoveToExistingCollectionDialog = (resource: ContextMenuResource) =>
117     (dispatch: Dispatch, getState: () => RootState) => {
118         const sourceCollection = getState().collectionPanel.item;
119
120         if (sourceCollection) {
121             openMoveToExistingDialog(dispatch, sourceCollection, [resource]);
122         }
123     };
124
125 export const openCollectionPartialMoveMultipleToExistingCollectionDialog = () =>
126     (dispatch: Dispatch, getState: () => RootState) => {
127         const sourceCollection = getState().collectionPanel.item;
128         const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
129
130         if (sourceCollection && selectedItems.length) {
131             openMoveToExistingDialog(dispatch, sourceCollection, selectedItems);
132         }
133     };
134
135 const openMoveToExistingDialog = (dispatch: Dispatch, sourceCollection: CollectionResource, selectedItems: (CollectionPanelDirectory | CollectionPanelFile | ContextMenuResource)[]) => {
136     // Get selected files
137     const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
138     // Populate form initial state
139     const initialFormData = {
140         destination: {uuid: sourceCollection.uuid, path: ''}
141     };
142     dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialFormData));
143     dispatch<any>(resetPickerProjectTree());
144     dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: collectionFileSelection }));
145 }
146
147 export const moveCollectionPartialToExistingCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToExistingCollectionFormData) =>
148     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
149         if (fileSelection.collection && formData.destination && formData.destination.uuid) {
150             try {
151                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
152                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
153
154                 // Move files
155                 const updatedCollection = await services.collectionService.moveFiles(
156                     fileSelection.collection.uuid,
157                     fileSelection.collection.portableDataHash,
158                     fileSelection.selectedPaths,
159                     {uuid: formData.destination.uuid},
160                     formData.destination.path || '/', false
161                 );
162                 dispatch(updateResources([updatedCollection]));
163
164                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
165                 dispatch(snackbarActions.OPEN_SNACKBAR({
166                     message: 'Files have been moved to selected collection.',
167                     hideDuration: 2000,
168                     kind: SnackbarKind.SUCCESS
169                 }));
170                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
171             } catch (e) {
172                 const error = getCommonResourceServiceError(e);
173                 if (error === CommonResourceServiceError.UNKNOWN) {
174                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
175                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
176                 }
177                 dispatch(stopSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
178                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
179             }
180         }
181     };
182
183 export const openCollectionPartialMoveToSeparateCollectionsDialog = () =>
184     (dispatch: Dispatch, getState: () => RootState) => {
185         const sourceCollection = getState().collectionPanel.item;
186         const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
187
188         if (sourceCollection && selectedItems.length) {
189             // Get selected files
190             const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
191             // Populate form initial state
192             const initialData = {
193                 name: sourceCollection.name,
194                 projectUuid: undefined
195             };
196             dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, initialData));
197             dispatch<any>(resetPickerProjectTree());
198             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, data: collectionFileSelection }));
199         }
200     };
201
202 export const moveCollectionPartialToSeparateCollections = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToSeparateCollectionsFormData) =>
203     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
204         if (fileSelection.collection) {
205             try {
206                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
207                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
208
209                 // Move files
210                 const collections = await Promise.all(fileSelection.selectedPaths.map((path) =>
211                     services.collectionService.moveFiles(
212                         fileSelection.collection.uuid,
213                         fileSelection.collection.portableDataHash,
214                         [path],
215                         {
216                             name: `File moved from collection ${formData.name}${path}`,
217                             ownerUuid: formData.projectUuid,
218                             uuid: undefined,
219                         },
220                         '/',
221                         false
222                     )
223                 ));
224                 dispatch(updateResources(collections));
225                 dispatch<any>(navigateTo(formData.projectUuid));
226
227                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
228                 dispatch(snackbarActions.OPEN_SNACKBAR({
229                     message: 'New collections created.',
230                     hideDuration: 2000,
231                     kind: SnackbarKind.SUCCESS
232                 }));
233                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
234             } catch (e) {
235                 const error = getCommonResourceServiceError(e);
236                 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
237                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection from one or more files already exists', hideDuration: 2000, kind: SnackbarKind.ERROR }));
238                 } else if (error === CommonResourceServiceError.UNKNOWN) {
239                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
240                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
241                 } else {
242                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
243                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
244                 }
245                 dispatch(stopSubmit(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
246                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
247             }
248         }
249     };