20031: Add ability to create and update collections during replace_files, moveFiles...
[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                 // Get selected files
59                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
60                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
61
62                 // Move files
63                 const updatedCollection = await services.collectionService.moveFiles(
64                     sourceCollection.uuid,
65                     sourceCollection.portableDataHash,
66                     paths,
67                     {
68                         name,
69                         description,
70                         ownerUuid: projectUuid,
71                         uuid: undefined,
72                     },
73                     '/',
74                     false
75                 );
76                 dispatch(updateResources([updatedCollection]));
77
78                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
79                 dispatch(snackbarActions.OPEN_SNACKBAR({
80                     message: 'Files have been moved to selected collection.',
81                     hideDuration: 2000,
82                     kind: SnackbarKind.SUCCESS
83                 }));
84                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
85             } catch (e) {
86                 const error = getCommonResourceServiceError(e);
87                 if (error === CommonResourceServiceError.UNKNOWN) {
88                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
89                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
90                 }
91                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
92             }
93         }
94     };
95
96 export const openCollectionPartialMoveToExistingCollectionDialog = () =>
97     (dispatch: Dispatch, getState: () => RootState) => {
98         const currentCollection = getState().collectionPanel.item;
99         if (currentCollection) {
100             const initialData = {
101                 collectionUuid: ''
102             };
103             dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialData));
104             dispatch<any>(resetPickerProjectTree());
105             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
106             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: {} }));
107         }
108     };
109
110 export const moveCollectionPartialToExistingCollection = ({ collectionUuid }: CollectionPartialMoveToExistingCollectionFormData) =>
111     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
112         const state = getState();
113         // Get current collection
114         const sourceCollection = state.collectionPanel.item;
115
116         if (sourceCollection) {
117             try {
118                 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
119                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
120                 // Get selected files
121                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
122                     .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
123
124                 // Move files
125                 const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, {uuid: collectionUuid}, '/', false);
126                 dispatch(updateResources([updatedCollection]));
127
128                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
129                 dispatch(snackbarActions.OPEN_SNACKBAR({
130                     message: 'Files have been moved to selected collection.',
131                     hideDuration: 2000,
132                     kind: SnackbarKind.SUCCESS
133                 }));
134                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
135             } catch (e) {
136                 const error = getCommonResourceServiceError(e);
137                 if (error === CommonResourceServiceError.UNKNOWN) {
138                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
139                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
140                 }
141                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
142             }
143         }
144     };