1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
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';
25 export interface CollectionPartialMoveToNewCollectionFormData {
31 export interface CollectionPartialMoveToExistingCollectionFormData {
32 destination: FileOperationLocation;
35 export interface CollectionPartialMoveToSeparateCollectionsFormData {
40 export const openCollectionPartialMoveToNewCollectionDialog = (resource: ContextMenuResource) =>
41 (dispatch: Dispatch, getState: () => RootState) => {
42 const sourceCollection = getState().collectionPanel.item;
44 if (sourceCollection) {
45 openMoveToNewDialog(dispatch, sourceCollection, [resource]);
49 export const openCollectionPartialMoveMultipleToNewCollectionDialog = () =>
50 (dispatch: Dispatch, getState: () => RootState) => {
51 const sourceCollection = getState().collectionPanel.item;
52 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
54 if (sourceCollection && selectedItems.length) {
55 openMoveToNewDialog(dispatch, sourceCollection, selectedItems);
59 const openMoveToNewDialog = (dispatch: Dispatch, sourceCollection: CollectionResource, selectedItems: (CollectionPanelDirectory | CollectionPanelFile | ContextMenuResource)[]) => {
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
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 }));
73 export const moveCollectionPartialToNewCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToNewCollectionFormData) =>
74 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75 if (fileSelection.collection) {
77 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
78 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
81 const updatedCollection = await services.collectionService.moveFiles(
82 fileSelection.collection.uuid,
83 fileSelection.collection.portableDataHash,
84 fileSelection.selectedPaths,
87 description: formData.description,
88 ownerUuid: formData.projectUuid,
94 dispatch(updateResources([updatedCollection]));
95 dispatch<any>(navigateTo(updatedCollection.uuid));
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.',
101 kind: SnackbarKind.SUCCESS
103 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
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 }));
110 dispatch(stopSubmit(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
111 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
116 export const openCollectionPartialMoveToExistingCollectionDialog = (resource: ContextMenuResource) =>
117 (dispatch: Dispatch, getState: () => RootState) => {
118 const sourceCollection = getState().collectionPanel.item;
120 if (sourceCollection) {
121 openMoveToExistingDialog(dispatch, sourceCollection, [resource]);
125 export const openCollectionPartialMoveMultipleToExistingCollectionDialog = () =>
126 (dispatch: Dispatch, getState: () => RootState) => {
127 const sourceCollection = getState().collectionPanel.item;
128 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
130 if (sourceCollection && selectedItems.length) {
131 openMoveToExistingDialog(dispatch, sourceCollection, selectedItems);
135 const openMoveToExistingDialog = (dispatch: Dispatch, sourceCollection: CollectionResource, selectedItems: (CollectionPanelDirectory | CollectionPanelFile | ContextMenuResource)[]) => {
136 // Get selected files
137 const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
138 // Populate form initial state
139 const initialFormData = {
140 destination: {uuid: sourceCollection.uuid, path: ''}
142 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialFormData));
143 dispatch<any>(resetPickerProjectTree());
144 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: collectionFileSelection }));
147 export const moveCollectionPartialToExistingCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToExistingCollectionFormData) =>
148 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
149 if (fileSelection.collection && formData.destination && formData.destination.uuid) {
151 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
152 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
155 const updatedCollection = await services.collectionService.moveFiles(
156 fileSelection.collection.uuid,
157 fileSelection.collection.portableDataHash,
158 fileSelection.selectedPaths,
159 {uuid: formData.destination.uuid},
160 formData.destination.path || '/', false
162 dispatch(updateResources([updatedCollection]));
164 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
165 dispatch(snackbarActions.OPEN_SNACKBAR({
166 message: 'Files have been moved to selected collection.',
168 kind: SnackbarKind.SUCCESS
170 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
172 const error = getCommonResourceServiceError(e);
173 if (error === CommonResourceServiceError.UNKNOWN) {
174 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
175 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
177 dispatch(stopSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
178 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
183 export const openCollectionPartialMoveToSeparateCollectionsDialog = () =>
184 (dispatch: Dispatch, getState: () => RootState) => {
185 const sourceCollection = getState().collectionPanel.item;
186 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
188 if (sourceCollection && selectedItems.length) {
189 // Get selected files
190 const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
191 // Populate form initial state
192 const initialData = {
193 name: sourceCollection.name,
194 projectUuid: undefined
196 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, initialData));
197 dispatch<any>(resetPickerProjectTree());
198 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, data: collectionFileSelection }));
202 export const moveCollectionPartialToSeparateCollections = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToSeparateCollectionsFormData) =>
203 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
204 if (fileSelection.collection) {
206 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
207 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
210 const collections = await Promise.all(fileSelection.selectedPaths.map((path) =>
211 services.collectionService.moveFiles(
212 fileSelection.collection.uuid,
213 fileSelection.collection.portableDataHash,
216 name: `File moved from collection ${formData.name}${path}`,
217 ownerUuid: formData.projectUuid,
224 dispatch(updateResources(collections));
225 dispatch<any>(navigateTo(formData.projectUuid));
227 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
228 dispatch(snackbarActions.OPEN_SNACKBAR({
229 message: 'New collections created.',
231 kind: SnackbarKind.SUCCESS
233 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
235 const error = getCommonResourceServiceError(e);
236 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
237 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection from one or more files already exists', hideDuration: 2000, kind: SnackbarKind.ERROR }));
238 } else if (error === CommonResourceServiceError.UNKNOWN) {
239 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
240 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
242 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
243 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
245 dispatch(stopSubmit(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
246 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));