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 } 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(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
115 export const openCollectionPartialMoveToExistingCollectionDialog = (resource: ContextMenuResource) =>
116 (dispatch: Dispatch, getState: () => RootState) => {
117 const sourceCollection = getState().collectionPanel.item;
119 if (sourceCollection) {
120 openMoveToExistingDialog(dispatch, sourceCollection, [resource]);
124 export const openCollectionPartialMoveMultipleToExistingCollectionDialog = () =>
125 (dispatch: Dispatch, getState: () => RootState) => {
126 const sourceCollection = getState().collectionPanel.item;
127 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
129 if (sourceCollection && selectedItems.length) {
130 openMoveToExistingDialog(dispatch, sourceCollection, selectedItems);
134 const openMoveToExistingDialog = (dispatch: Dispatch, sourceCollection: CollectionResource, selectedItems: (CollectionPanelDirectory | CollectionPanelFile | ContextMenuResource)[]) => {
135 // Get selected files
136 const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
137 // Populate form initial state
138 const initialFormData = {
139 destination: {uuid: sourceCollection.uuid, path: ''}
141 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialFormData));
142 dispatch<any>(resetPickerProjectTree());
143 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: collectionFileSelection }));
146 export const moveCollectionPartialToExistingCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToExistingCollectionFormData) =>
147 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
148 if (fileSelection.collection && formData.destination && formData.destination.uuid) {
150 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
151 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
154 const updatedCollection = await services.collectionService.moveFiles(
155 fileSelection.collection.uuid,
156 fileSelection.collection.portableDataHash,
157 fileSelection.selectedPaths,
158 {uuid: formData.destination.uuid},
159 formData.destination.path || '/', false
161 dispatch(updateResources([updatedCollection]));
163 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
164 dispatch(snackbarActions.OPEN_SNACKBAR({
165 message: 'Files have been moved to selected collection.',
167 kind: SnackbarKind.SUCCESS
169 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
171 const error = getCommonResourceServiceError(e);
172 if (error === CommonResourceServiceError.UNKNOWN) {
173 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
174 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
176 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
181 export const openCollectionPartialMoveToSeparateCollectionsDialog = () =>
182 (dispatch: Dispatch, getState: () => RootState) => {
183 const sourceCollection = getState().collectionPanel.item;
184 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
186 if (sourceCollection && selectedItems.length) {
187 // Get selected files
188 const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
189 // Populate form initial state
190 const initialData = {
191 name: sourceCollection.name,
192 projectUuid: undefined
194 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, initialData));
195 dispatch<any>(resetPickerProjectTree());
196 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS, data: collectionFileSelection }));
200 export const moveCollectionPartialToSeparateCollections = (fileSelection: CollectionFileSelection, formData: CollectionPartialMoveToSeparateCollectionsFormData) =>
201 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
202 if (fileSelection.collection) {
204 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
205 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
208 const collections = await Promise.all(fileSelection.selectedPaths.map((path) =>
209 services.collectionService.moveFiles(
210 fileSelection.collection.uuid,
211 fileSelection.collection.portableDataHash,
214 name: `File moved from collection ${formData.name}${path}`,
215 ownerUuid: formData.projectUuid,
222 dispatch(updateResources(collections));
223 dispatch<any>(navigateTo(formData.projectUuid));
225 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
226 dispatch(snackbarActions.OPEN_SNACKBAR({
227 message: 'New collections created.',
229 kind: SnackbarKind.SUCCESS
231 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));
233 const error = getCommonResourceServiceError(e);
234 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
235 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection from one or more files already exists', hideDuration: 2000, kind: SnackbarKind.ERROR }));
236 } else if (error === CommonResourceServiceError.UNKNOWN) {
237 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
238 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
240 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS }));
241 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
243 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SEPARATE_COLLECTIONS));