20031: Add collection partial move/copy to new/existing collection
[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 { filterCollectionFilesBySelection } from "store/collection-panel/collection-panel-files/collection-panel-files-state";
10 import { dialogActions } from "store/dialog/dialog-actions";
11 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
12 import { resetPickerProjectTree } from "store/project-tree-picker/project-tree-picker-actions";
13 import { updateResources } from "store/resources/resources-actions";
14 import { SnackbarKind, snackbarActions } from "store/snackbar/snackbar-actions";
15 import { RootState } from "store/store";
16 import { initProjectsTreePicker } from "store/tree-picker/tree-picker-actions";
17
18 export const COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION_DIALOG';
19 export const COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION_DIALOG';
20
21 export interface CollectionPartialMoveToNewCollectionFormData {
22     name: string;
23     description: string;
24     projectUuid: string;
25 }
26
27 export interface CollectionPartialMoveToExistingCollectionFormData {
28     collectionUuid: string;
29 }
30
31 export const openCollectionPartialMoveToNewCollectionDialog = () =>
32     (dispatch: Dispatch, getState: () => RootState) => {
33         const currentCollection = getState().collectionPanel.item;
34         if (currentCollection) {
35             const initialData = {
36                 name: `Files moved from: ${currentCollection.name}`,
37                 description: currentCollection.description,
38                 projectUuid: undefined
39             };
40             dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, initialData));
41             dispatch<any>(resetPickerProjectTree());
42             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
43             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, data: {} }));
44         }
45     };
46
47 export const moveCollectionPartialToNewCollection = ({ name, description, projectUuid }: CollectionPartialMoveToNewCollectionFormData) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49         const state = getState();
50         // Get current collection
51         const sourceCollection = state.collectionPanel.item;
52
53         if (sourceCollection) {
54             try {
55                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
56                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
57
58                 // Create new collection
59                 const newCollection = await services.collectionService.create({
60                     name,
61                     description,
62                     ownerUuid: projectUuid,
63                     uuid: undefined,
64                 });
65
66                 // Get selected files
67                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
68                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
69
70                 // Move files
71                 const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, newCollection.uuid, '/', false);
72                 dispatch(updateResources([updatedCollection]));
73
74                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
75                 dispatch(snackbarActions.OPEN_SNACKBAR({
76                     message: 'Files have been moved to selected collection.',
77                     hideDuration: 2000,
78                     kind: SnackbarKind.SUCCESS
79                 }));
80                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
81             } catch (e) {
82                 const error = getCommonResourceServiceError(e);
83                 if (error === CommonResourceServiceError.UNKNOWN) {
84                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
85                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
86                 }
87                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
88             }
89         }
90     };
91
92 export const openCollectionPartialMoveToExistingCollectionDialog = () =>
93     (dispatch: Dispatch, getState: () => RootState) => {
94         const currentCollection = getState().collectionPanel.item;
95         if (currentCollection) {
96             const initialData = {
97                 collectionUuid: ''
98             };
99             dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialData));
100             dispatch<any>(resetPickerProjectTree());
101             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
102             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: {} }));
103         }
104     };
105
106 export const moveCollectionPartialToExistingCollection = ({ collectionUuid }: CollectionPartialMoveToExistingCollectionFormData) =>
107     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
108         const state = getState();
109         // Get current collection
110         const sourceCollection = state.collectionPanel.item;
111
112         if (sourceCollection) {
113             try {
114                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
115                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
116                 // Get selected files
117                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
118                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
119
120                 // Move files
121                 const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, collectionUuid, '/', false);
122                 dispatch(updateResources([updatedCollection]));
123
124                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
125                 dispatch(snackbarActions.OPEN_SNACKBAR({
126                     message: 'Files have been moved to selected collection.',
127                     hideDuration: 2000,
128                     kind: SnackbarKind.SUCCESS
129                 }));
130                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
131             } catch (e) {
132                 const error = getCommonResourceServiceError(e);
133                 if (error === CommonResourceServiceError.UNKNOWN) {
134                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
135                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
136                 }
137                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
138             }
139         }
140     };