20031: Add single file context menu move/copy actions
[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 } 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(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
111             }
112         }
113     };
114
115 export const openCollectionPartialMoveToExistingCollectionDialog = (resource: ContextMenuResource) =>
116     (dispatch: Dispatch, getState: () => RootState) => {
117         const sourceCollection = getState().collectionPanel.item;
118
119         if (sourceCollection) {
120             openMoveToExistingDialog(dispatch, sourceCollection, [resource]);
121         }
122     };
123
124 export const openCollectionPartialMoveMultipleToExistingCollectionDialog = () =>
125     (dispatch: Dispatch, getState: () => RootState) => {
126         const sourceCollection = getState().collectionPanel.item;
127         const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
128
129         if (sourceCollection && selectedItems.length) {
130             openMoveToExistingDialog(dispatch, sourceCollection, selectedItems);
131         }
132     };
133
134 const openMoveToExistingDialog = (dispatch: Dispatch, sourceCollection: CollectionResource, selectedItems: (CollectionPanelDirectory | CollectionPanelFile | ContextMenuResource)[]) => {
135     // Get selected files
136     const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
137     // Populate form initial state
138     const initialFormData = {
139         destination: {uuid: sourceCollection.uuid, path: ''}
140     };
141     dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialFormData));
142     dispatch<any>(resetPickerProjectTree());
143     dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: collectionFileSelection }));
144 }
145
146 export const moveCollectionPartialToExistingCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToExistingCollectionFormData) =>
147     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
148         if (fileSelection.collection && formData.destination && formData.destination.uuid) {
149             try {
150                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
151                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
152
153                 // Move files
154                 const updatedCollection = await services.collectionService.moveFiles(
155                     fileSelection.collection.uuid,
156                     fileSelection.collection.portableDataHash,
157                     fileSelection.selectedPaths,
158                     {uuid: formData.destination.uuid},
159                     formData.destination.path || '/', false
160                 );
161                 dispatch(updateResources([updatedCollection]));
162
163                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
164                 dispatch(snackbarActions.OPEN_SNACKBAR({
165                     message: 'Files have been moved to selected collection.',
166                     hideDuration: 2000,
167                     kind: SnackbarKind.SUCCESS
168                 }));
169                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
170             } catch (e) {
171                 const error = getCommonResourceServiceError(e);
172                 if (error === CommonResourceServiceError.UNKNOWN) {
173                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
174                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
175                 }
176                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
177             }
178         }
179     };
180
181 export const openCollectionPartialMoveToSeparateCollectionsDialog = () =>
182     (dispatch: Dispatch, getState: () => RootState) => {
183         const sourceCollection = getState().collectionPanel.item;
184         const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
185
186         if (sourceCollection && selectedItems.length) {
187             // Get selected files
188             const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
189             // Populate form initial state
190             const initialData = {
191                 name: sourceCollection.name,
192                 projectUuid: undefined
193             };
194             dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, initialData));
195             dispatch<any>(resetPickerProjectTree());
196             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, data: collectionFileSelection }));
197         }
198     };
199
200 export const moveCollectionPartialToSeparateCollections = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToSeparateCollectionsFormData) =>
201     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
202         if (fileSelection.collection) {
203             try {
204                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
205                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
206
207                 // Move files
208                 const collections = await Promise.all(fileSelection.selectedPaths.map((path) =>
209                     services.collectionService.moveFiles(
210                         fileSelection.collection.uuid,
211                         fileSelection.collection.portableDataHash,
212                         [path],
213                         {
214                             name: `File moved from collection ${formData.name}${path}`,
215                             ownerUuid: formData.projectUuid,
216                             uuid: undefined,
217                         },
218                         '/',
219                         false
220                     )
221                 ));
222                 dispatch(updateResources(collections));
223                 dispatch<any>(navigateTo(formData.projectUuid));
224
225                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
226                 dispatch(snackbarActions.OPEN_SNACKBAR({
227                     message: 'New collections created.',
228                     hideDuration: 2000,
229                     kind: SnackbarKind.SUCCESS
230                 }));
231                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
232             } catch (e) {
233                 const error = getCommonResourceServiceError(e);
234                 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
235                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection from one or more files already exists', hideDuration: 2000, kind: SnackbarKind.ERROR }));
236                 } else if (error === CommonResourceServiceError.UNKNOWN) {
237                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
238                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
239                 } else {
240                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
241                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
242                 }
243                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
244             }
245         }
246     };