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