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 { FormErrors, 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 { filterCollectionFilesBySelection } 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';
19 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
20 export const COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_COPY_TO_SELECTED_DIALOG';
21 export const COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS = 'COLLECTION_PARTIAL_COPY_TO_SEPARATE_DIALOG';
23 export interface CollectionPartialCopyToNewCollectionFormData {
29 export interface CollectionPartialCopyToExistingCollectionFormData {
30 destination: FileOperationLocation;
33 export interface CollectionPartialCopyToSeparateCollectionsFormData {
38 export const openCollectionPartialCopyToNewCollectionDialog = () =>
39 (dispatch: Dispatch, getState: () => RootState) => {
40 const currentCollection = getState().collectionPanel.item;
41 if (currentCollection) {
43 name: `Files extracted from: ${currentCollection.name}`,
44 description: currentCollection.description,
45 projectUuid: undefined
47 dispatch(initialize(COLLECTION_PARTIAL_COPY_FORM_NAME, initialData));
48 dispatch<any>(resetPickerProjectTree());
49 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME, data: {} }));
53 export const copyCollectionPartialToNewCollection = ({ name, description, projectUuid }: CollectionPartialCopyToNewCollectionFormData) =>
54 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55 const state = getState();
56 // Get current collection
57 const sourceCollection = state.collectionPanel.item;
59 if (sourceCollection) {
61 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
62 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
65 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
66 .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
69 const updatedCollection = await services.collectionService.copyFiles(
70 sourceCollection.portableDataHash,
75 ownerUuid: projectUuid,
81 dispatch(updateResources([updatedCollection]));
82 dispatch<any>(navigateTo(updatedCollection.uuid))
84 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
85 dispatch(snackbarActions.OPEN_SNACKBAR({
86 message: 'New collection created.',
88 kind: SnackbarKind.SUCCESS
90 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
92 const error = getCommonResourceServiceError(e);
93 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
94 dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME, { name: 'Collection with this name already exists.' } as FormErrors));
95 } else if (error === CommonResourceServiceError.UNKNOWN) {
96 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
97 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
99 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
100 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
102 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
107 export const openCollectionPartialCopyToExistingCollectionDialog = () =>
108 (dispatch: Dispatch, getState: () => RootState) => {
109 const currentCollection = getState().collectionPanel.item;
110 if (currentCollection) {
111 const initialData = {
112 destination: {uuid: currentCollection.uuid, destinationPath: ''}
114 dispatch(initialize(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, initialData));
115 dispatch<any>(resetPickerProjectTree());
116 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, data: {} }));
120 export const copyCollectionPartialToExistingCollection = ({ destination }: CollectionPartialCopyToExistingCollectionFormData) =>
121 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
122 const state = getState();
123 // Get current collection
124 const sourceCollection = state.collectionPanel.item;
126 if (sourceCollection && destination && destination.uuid) {
128 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
129 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
130 // Get selected files
131 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
132 .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
135 const updatedCollection = await services.collectionService.copyFiles(sourceCollection.portableDataHash, paths, {uuid: destination.uuid}, destination.path || '/', false);
136 dispatch(updateResources([updatedCollection]));
138 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
139 dispatch(snackbarActions.OPEN_SNACKBAR({
140 message: 'Files has been copied to selected collection.',
142 kind: SnackbarKind.SUCCESS
144 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
146 const error = getCommonResourceServiceError(e);
147 if (error === CommonResourceServiceError.UNKNOWN) {
148 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
149 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
151 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
156 export const openCollectionPartialCopyToSeparateCollectionsDialog = () =>
157 (dispatch: Dispatch, getState: () => RootState) => {
158 const currentCollection = getState().collectionPanel.item;
159 if (currentCollection) {
160 const initialData = {
161 name: currentCollection.name,
162 projectUuid: undefined
164 dispatch(initialize(COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS, initialData));
165 dispatch<any>(resetPickerProjectTree());
166 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SEPARATE_COLLECTIONS, data: {} }));
170 export const copyCollectionPartialToSeparateCollections = ({ name, projectUuid }: CollectionPartialCopyToSeparateCollectionsFormData) =>
171 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
172 const state = getState();
173 // Get current collection
174 const sourceCollection = state.collectionPanel.item;
176 if (sourceCollection) {
178 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
179 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
181 // Get selected files
182 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
183 .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
186 const collections = await Promise.all(paths.map((path) =>
187 services.collectionService.copyFiles(
188 sourceCollection.portableDataHash,
191 name: `File split from collection ${name}${path}`,
192 ownerUuid: projectUuid,
199 dispatch(updateResources(collections));
201 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
202 dispatch(snackbarActions.OPEN_SNACKBAR({
203 message: 'New collections created.',
205 kind: SnackbarKind.SUCCESS
207 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
209 const error = getCommonResourceServiceError(e);
210 console.log(e, error);
211 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
212 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection from one or more files already exists', hideDuration: 2000, kind: SnackbarKind.ERROR }));
213 } else if (error === CommonResourceServiceError.UNKNOWN) {
214 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
215 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
217 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
218 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
220 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));