20031: Add split files into separate collection 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 { FormErrors, 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 { filterCollectionFilesBySelection } from "store/collection-panel/collection-panel-files/collection-panel-files-state";
10 import { dialogActions } from "store/dialog/dialog-actions";
11 import { navigateTo } from "store/navigation/navigation-action";
12 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
13 import { resetPickerProjectTree } from "store/project-tree-picker/project-tree-picker-actions";
14 import { updateResources } from "store/resources/resources-actions";
15 import { SnackbarKind, snackbarActions } from "store/snackbar/snackbar-actions";
16 import { RootState } from "store/store";
17 import { initProjectsTreePicker } from "store/tree-picker/tree-picker-actions";
18
19 export const COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_NEW_DIALOG';
20 export const COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_SELECTED_DIALOG';
21 export const COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS = 'COLLECTION_PARTIAL_MOVE_TO_SEPARATE_DIALOG';
22
23 export interface CollectionPartialMoveToNewCollectionFormData {
24     name: string;
25     description: string;
26     projectUuid: string;
27 }
28
29 export interface CollectionPartialMoveToExistingCollectionFormData {
30     destination: {uuid: string, path?: string};
31 }
32
33 export interface CollectionPartialMoveToSeparateCollectionsFormData {
34     name: string;
35     projectUuid: string;
36 }
37
38 export const openCollectionPartialMoveToNewCollectionDialog = () =>
39     (dispatch: Dispatch, getState: () => RootState) => {
40         const currentCollection = getState().collectionPanel.item;
41         if (currentCollection) {
42             const initialData = {
43                 name: `Files moved from: ${currentCollection.name}`,
44                 description: currentCollection.description,
45                 projectUuid: undefined
46             };
47             dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, initialData));
48             dispatch<any>(resetPickerProjectTree());
49             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
50             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, data: {} }));
51         }
52     };
53
54 export const moveCollectionPartialToNewCollection = ({ name, description, projectUuid }: CollectionPartialMoveToNewCollectionFormData) =>
55     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56         const state = getState();
57         // Get current collection
58         const sourceCollection = state.collectionPanel.item;
59
60         if (sourceCollection) {
61             try {
62                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
63                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
64
65                 // Get selected files
66                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
67                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
68
69                 // Move files
70                 const updatedCollection = await services.collectionService.moveFiles(
71                     sourceCollection.uuid,
72                     sourceCollection.portableDataHash,
73                     paths,
74                     {
75                         name,
76                         description,
77                         ownerUuid: projectUuid,
78                         uuid: undefined,
79                     },
80                     '/',
81                     false
82                 );
83                 dispatch(updateResources([updatedCollection]));
84                 dispatch<any>(navigateTo(updatedCollection.uuid))
85
86                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
87                 dispatch(snackbarActions.OPEN_SNACKBAR({
88                     message: 'Files have been moved to selected collection.',
89                     hideDuration: 2000,
90                     kind: SnackbarKind.SUCCESS
91                 }));
92                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
93             } catch (e) {
94                 const error = getCommonResourceServiceError(e);
95                 if (error === CommonResourceServiceError.UNKNOWN) {
96                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
97                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
98                 }
99                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
100             }
101         }
102     };
103
104 export const openCollectionPartialMoveToExistingCollectionDialog = () =>
105     (dispatch: Dispatch, getState: () => RootState) => {
106         const currentCollection = getState().collectionPanel.item;
107         if (currentCollection) {
108             const initialData = {
109                 destination: {uuid: '', path: ''}
110             };
111             dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialData));
112             dispatch<any>(resetPickerProjectTree());
113             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
114             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: {} }));
115         }
116     };
117
118 export const moveCollectionPartialToExistingCollection = ({ destination }: CollectionPartialMoveToExistingCollectionFormData) =>
119     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
120         const state = getState();
121         // Get current collection
122         const sourceCollection = state.collectionPanel.item;
123
124         if (sourceCollection && destination.uuid) {
125             try {
126                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
127                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
128                 // Get selected files
129                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
130                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
131
132                 // Move files
133                 const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, {uuid: destination.uuid}, destination.path || '/', false);
134                 dispatch(updateResources([updatedCollection]));
135
136                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
137                 dispatch(snackbarActions.OPEN_SNACKBAR({
138                     message: 'Files have been moved to selected collection.',
139                     hideDuration: 2000,
140                     kind: SnackbarKind.SUCCESS
141                 }));
142                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
143             } catch (e) {
144                 const error = getCommonResourceServiceError(e);
145                 if (error === CommonResourceServiceError.UNKNOWN) {
146                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
147                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
148                 }
149                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
150             }
151         }
152     };
153
154 export const openCollectionPartialMoveToSeparateCollectionsDialog = () =>
155     (dispatch: Dispatch, getState: () => RootState) => {
156         const currentCollection = getState().collectionPanel.item;
157         if (currentCollection) {
158             const initialData = {
159                 name: currentCollection.name,
160                 projectUuid: undefined
161             };
162             dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, initialData));
163             dispatch<any>(resetPickerProjectTree());
164             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
165             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, data: {} }));
166         }
167     };
168
169 export const moveCollectionPartialToSeparateCollections = ({ name, projectUuid }: CollectionPartialMoveToSeparateCollectionsFormData) =>
170     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
171         const state = getState();
172         // Get current collection
173         const sourceCollection = state.collectionPanel.item;
174
175         if (sourceCollection) {
176             try {
177                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
178                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
179
180                 // Get selected files
181                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
182                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
183
184                 // Move files
185                 const collections = await Promise.all(paths.map((path) =>
186                     services.collectionService.moveFiles(
187                         sourceCollection.uuid,
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_MOVE_TO_SEPARATE_COLLECTIONS }));
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_MOVE_TO_SEPARATE_COLLECTIONS));
208             } catch (e) {
209                 const error = getCommonResourceServiceError(e);
210                 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
211                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection from one or more files already exists', hideDuration: 2000, kind: SnackbarKind.ERROR }));
212                 } else if (error === CommonResourceServiceError.UNKNOWN) {
213                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
214                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
215                 } else {
216                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
217                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
218                 }
219                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
220             }
221         }
222     };