1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from 'redux';
6 import { RootState } from 'store/store';
7 import { initialize, startSubmit, stopSubmit } from 'redux-form';
8 import { resetPickerProjectTree } from 'store/project-tree-picker/project-tree-picker-actions';
9 import { dialogActions } from 'store/dialog/dialog-actions';
10 import { ServiceRepository } from 'services/services';
11 import { CollectionFileSelection, CollectionPanelDirectory, CollectionPanelFile, filterCollectionFilesBySelection, getCollectionSelection } from '../collection-panel/collection-panel-files/collection-panel-files-state';
12 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
13 import { getCommonResourceServiceError, CommonResourceServiceError } from 'services/common-service/common-resource-service';
14 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
15 import { FileOperationLocation } from "store/tree-picker/tree-picker-actions";
16 import { updateResources } from 'store/resources/resources-actions';
17 import { navigateTo } from 'store/navigation/navigation-action';
18 import { ContextMenuResource } from 'store/context-menu/context-menu-actions';
19 import { CollectionResource } from 'models/collection';
21 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
22 export const COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_COPY_TO_SELECTED_DIALOG';
23 export const COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS = 'COLLECTION_PARTIAL_COPY_TO_SEPARATE_DIALOG';
25 export interface CollectionPartialCopyToNewCollectionFormData {
31 export interface CollectionPartialCopyToExistingCollectionFormData {
32 destination: FileOperationLocation;
35 export interface CollectionPartialCopyToSeparateCollectionsFormData {
40 export const openCollectionPartialCopyToNewCollectionDialog = (resource: ContextMenuResource) =>
41 (dispatch: Dispatch, getState: () => RootState) => {
42 const sourceCollection = getState().collectionPanel.item;
44 if (sourceCollection) {
45 openCopyToNewDialog(dispatch, sourceCollection, [resource]);
49 export const openCollectionPartialCopyMultipleToNewCollectionDialog = () =>
50 (dispatch: Dispatch, getState: () => RootState) => {
51 const sourceCollection = getState().collectionPanel.item;
52 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
54 if (sourceCollection && selectedItems.length) {
55 openCopyToNewDialog(dispatch, sourceCollection, selectedItems);
59 const openCopyToNewDialog = (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 extracted from: ${sourceCollection.name}`,
65 description: sourceCollection.description,
66 projectUuid: undefined
68 dispatch(initialize(COLLECTION_PARTIAL_COPY_FORM_NAME, initialFormData));
69 dispatch<any>(resetPickerProjectTree());
70 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME, data: collectionFileSelection }));
73 export const copyCollectionPartialToNewCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialCopyToNewCollectionFormData) =>
74 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75 if (fileSelection.collection) {
77 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
78 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
81 const updatedCollection = await services.collectionService.copyFiles(
82 fileSelection.collection.portableDataHash,
83 fileSelection.selectedPaths,
86 description: formData.description,
87 ownerUuid: formData.projectUuid,
93 dispatch(updateResources([updatedCollection]));
94 dispatch<any>(navigateTo(updatedCollection.uuid));
96 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
97 dispatch(snackbarActions.OPEN_SNACKBAR({
98 message: 'New collection created.',
100 kind: SnackbarKind.SUCCESS
103 const error = getCommonResourceServiceError(e);
104 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
105 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection with this name already exists', hideDuration: 2000, kind: SnackbarKind.ERROR }));
106 } else if (error === CommonResourceServiceError.UNKNOWN) {
107 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
108 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
110 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
111 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
114 dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
115 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
120 export const openCollectionPartialCopyToExistingCollectionDialog = (resource: ContextMenuResource) =>
121 (dispatch: Dispatch, getState: () => RootState) => {
122 const sourceCollection = getState().collectionPanel.item;
124 if (sourceCollection) {
125 openCopyToExistingDialog(dispatch, sourceCollection, [resource]);
129 export const openCollectionPartialCopyMultipleToExistingCollectionDialog = () =>
130 (dispatch: Dispatch, getState: () => RootState) => {
131 const sourceCollection = getState().collectionPanel.item;
132 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
134 if (sourceCollection && selectedItems.length) {
135 openCopyToExistingDialog(dispatch, sourceCollection, selectedItems);
139 const openCopyToExistingDialog = (dispatch: Dispatch, sourceCollection: CollectionResource, selectedItems: (CollectionPanelDirectory | CollectionPanelFile | ContextMenuResource)[]) => {
140 // Get selected files
141 const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
142 // Populate form initial state
143 const initialFormData = {
144 destination: {uuid: sourceCollection.uuid, destinationPath: ''}
146 dispatch(initialize(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, initialFormData));
147 dispatch<any>(resetPickerProjectTree());
148 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, data: collectionFileSelection }));
151 export const copyCollectionPartialToExistingCollection = (fileSelection: CollectionFileSelection, formData: CollectionPartialCopyToExistingCollectionFormData) =>
152 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
153 if (fileSelection.collection && formData.destination && formData.destination.uuid) {
155 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
156 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
159 const updatedCollection = await services.collectionService.copyFiles(
160 fileSelection.collection.portableDataHash,
161 fileSelection.selectedPaths,
162 {uuid: formData.destination.uuid},
163 formData.destination.subpath || '/',
166 dispatch(updateResources([updatedCollection]));
168 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
169 dispatch(snackbarActions.OPEN_SNACKBAR({
170 message: 'Files has been copied to selected collection.',
172 kind: SnackbarKind.SUCCESS
175 const error = getCommonResourceServiceError(e);
176 if (error === CommonResourceServiceError.UNKNOWN) {
177 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
178 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
181 dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
182 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
187 export const openCollectionPartialCopyToSeparateCollectionsDialog = () =>
188 (dispatch: Dispatch, getState: () => RootState) => {
189 const sourceCollection = getState().collectionPanel.item;
190 const selectedItems = filterCollectionFilesBySelection(getState().collectionPanelFiles, true);
192 if (sourceCollection && selectedItems.length) {
193 // Get selected files
194 const collectionFileSelection = getCollectionSelection(sourceCollection, selectedItems);
195 // Populate form initial state
196 const initialFormData = {
197 name: sourceCollection.name,
198 projectUuid: undefined
200 dispatch(initialize(COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS, initialFormData));
201 dispatch<any>(resetPickerProjectTree());
202 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS, data: collectionFileSelection }));
206 export const copyCollectionPartialToSeparateCollections = (fileSelection: CollectionFileSelection, formData: CollectionPartialCopyToSeparateCollectionsFormData) =>
207 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
208 if (fileSelection.collection) {
210 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS));
211 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS));
214 const collections = await Promise.all(fileSelection.selectedPaths.map((path) =>
215 services.collectionService.copyFiles(
216 fileSelection.collection.portableDataHash,
219 name: `File copied 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_COPY_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_COPY_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_COPY_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_COPY_TO_SEPARATE_COLLECTIONS));
249 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS));