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";
20 import { SOURCE_DESTINATION_EQUAL_ERROR_MESSAGE } from "services/collection-service/collection-service";
22 export const COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_NEW_DIALOG';
23 export const COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_SELECTED_DIALOG';
24 export const COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS = 'COLLECTION_PARTIAL_MOVE_TO_SEPARATE_DIALOG';
26 export interface CollectionPartialMoveToNewCollectionFormData {
32 export interface CollectionPartialMoveToExistingCollectionFormData {
33 destination: FileOperationLocation;
36 export interface CollectionPartialMoveToSeparateCollectionsFormData {
41 export const openCollectionPartialMoveToNewCollectionDialog = (resource: ContextMenuResource) =>
42 (dispatch: Dispatch, getState: () => RootState) => {
43 const sourceCollection = getState().collectionPanel.item;
45 if (sourceCollection) {
46 openMoveToNewDialog(dispatch, sourceCollection, [resource]);
50 export const openCollectionPartialMoveMultipleToNewCollectionDialog = () =>
51 (dispatch: Dispatch, getState: () => RootState) => {
52 const sourceCollection = getState().collectionPanel.item;
53 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
55 if (sourceCollection && selectedItems.length) {
56 openMoveToNewDialog(dispatch, sourceCollection, selectedItems);
60 const openMoveToNewDialog = (dispatch: Dispatch, sourceCollection: CollectionResource, selectedItems: (CollectionPanelDirectory | CollectionPanelFile | ContextMenuResource)[]) => {
62 const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
63 // Populate form initial state
64 const initialFormData = {
65 name: `Files moved from: ${sourceCollection.name}`,
66 description: sourceCollection.description,
67 projectUuid: undefined
69 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, initialFormData));
70 dispatch<any>(resetPickerProjectTree());
71 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, data: collectionFileSelection }));
74 export const moveCollectionPartialToNewCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToNewCollectionFormData) =>
75 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
76 if (fileSelection.collection) {
78 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
79 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
82 const updatedCollection = await services.collectionService.moveFiles(
83 fileSelection.collection.uuid,
84 fileSelection.collection.portableDataHash,
85 fileSelection.selectedPaths,
88 description: formData.description,
89 ownerUuid: formData.projectUuid,
95 dispatch(updateResources([updatedCollection]));
96 dispatch<any>(navigateTo(updatedCollection.uuid));
98 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
99 dispatch(snackbarActions.OPEN_SNACKBAR({
100 message: 'Files have been moved to selected collection.',
102 kind: SnackbarKind.SUCCESS
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 }));
111 dispatch(stopSubmit(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
112 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
117 export const openCollectionPartialMoveToExistingCollectionDialog = (resource: ContextMenuResource) =>
118 (dispatch: Dispatch, getState: () => RootState) => {
119 const sourceCollection = getState().collectionPanel.item;
121 if (sourceCollection) {
122 openMoveToExistingDialog(dispatch, sourceCollection, [resource]);
126 export const openCollectionPartialMoveMultipleToExistingCollectionDialog = () =>
127 (dispatch: Dispatch, getState: () => RootState) => {
128 const sourceCollection = getState().collectionPanel.item;
129 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
131 if (sourceCollection && selectedItems.length) {
132 openMoveToExistingDialog(dispatch, sourceCollection, selectedItems);
136 const openMoveToExistingDialog = (dispatch: Dispatch, sourceCollection: CollectionResource, selectedItems: (CollectionPanelDirectory | CollectionPanelFile | ContextMenuResource)[]) => {
137 // Get selected files
138 const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
139 // Populate form initial state
140 const initialFormData = {
141 destination: {uuid: sourceCollection.uuid, path: ''}
143 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialFormData));
144 dispatch<any>(resetPickerProjectTree());
145 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: collectionFileSelection }));
148 export const moveCollectionPartialToExistingCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToExistingCollectionFormData) =>
149 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
150 if (fileSelection.collection && formData.destination && formData.destination.uuid) {
152 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
153 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
156 const updatedCollection = await services.collectionService.moveFiles(
157 fileSelection.collection.uuid,
158 fileSelection.collection.portableDataHash,
159 fileSelection.selectedPaths,
160 {uuid: formData.destination.uuid},
161 formData.destination.subpath || '/', false
163 dispatch(updateResources([updatedCollection]));
165 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
166 dispatch(snackbarActions.OPEN_SNACKBAR({
167 message: 'Files have been moved to selected collection.',
169 kind: SnackbarKind.SUCCESS
172 const error = getCommonResourceServiceError(e);
173 if (error === CommonResourceServiceError.SOURCE_DESTINATION_CANNOT_BE_SAME) {
174 dispatch(snackbarActions.OPEN_SNACKBAR({ message: SOURCE_DESTINATION_EQUAL_ERROR_MESSAGE, hideDuration: 2000, kind: SnackbarKind.ERROR }));
175 } else if (error === CommonResourceServiceError.UNKNOWN) {
176 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
177 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
180 dispatch(stopSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
181 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
186 export const openCollectionPartialMoveToSeparateCollectionsDialog = () =>
187 (dispatch: Dispatch, getState: () => RootState) => {
188 const sourceCollection = getState().collectionPanel.item;
189 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
191 if (sourceCollection && selectedItems.length) {
192 // Get selected files
193 const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
194 // Populate form initial state
195 const initialData = {
196 name: sourceCollection.name,
197 projectUuid: undefined
199 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, initialData));
200 dispatch<any>(resetPickerProjectTree());
201 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, data: collectionFileSelection }));
205 export const moveCollectionPartialToSeparateCollections = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToSeparateCollectionsFormData) =>
206 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
207 if (fileSelection.collection) {
209 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
210 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
213 const collections = await Promise.all(fileSelection.selectedPaths.map((path) =>
214 services.collectionService.moveFiles(
215 fileSelection.collection.uuid,
216 fileSelection.collection.portableDataHash,
219 name: `File moved from collection ${formData.name}${path}`,
220 ownerUuid: formData.projectUuid,
227 dispatch(updateResources(collections));
228 dispatch<any>(navigateTo(formData.projectUuid));
230 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
231 dispatch(snackbarActions.OPEN_SNACKBAR({
232 message: 'New collections created.',
234 kind: SnackbarKind.SUCCESS
237 const error = getCommonResourceServiceError(e);
238 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
239 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection from one or more files already exists', hideDuration: 2000, kind: SnackbarKind.ERROR }));
240 } else if (error === CommonResourceServiceError.UNKNOWN) {
241 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
242 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
244 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
245 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
248 dispatch(stopSubmit(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
249 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));