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 { initProjectsTreePicker } from "store/tree-picker/tree-picker-actions";
16 import { updateResources } from 'store/resources/resources-actions';
18 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
19 export const COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_COPY_TO_SELECTED_DIALOG';
21 export interface CollectionPartialCopyToNewCollectionFormData {
27 export interface CollectionPartialCopyToExistingCollectionFormData {
28 collectionUuid: string;
31 export const openCollectionPartialCopyToNewCollectionDialog = () =>
32 (dispatch: Dispatch, getState: () => RootState) => {
33 const currentCollection = getState().collectionPanel.item;
34 if (currentCollection) {
36 name: `Files extracted from: ${currentCollection.name}`,
37 description: currentCollection.description,
38 projectUuid: undefined
40 dispatch(initialize(COLLECTION_PARTIAL_COPY_FORM_NAME, initialData));
41 dispatch<any>(resetPickerProjectTree());
42 dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_COPY_FORM_NAME));
43 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME, data: {} }));
47 export const copyCollectionPartialToNewCollection = ({ name, description, projectUuid }: CollectionPartialCopyToNewCollectionFormData) =>
48 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49 const state = getState();
50 // Get current collection
51 const sourceCollection = state.collectionPanel.item;
53 if (sourceCollection) {
55 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
56 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
59 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
60 .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
63 const updatedCollection = await services.collectionService.copyFiles(
64 sourceCollection.portableDataHash,
69 ownerUuid: projectUuid,
75 dispatch(updateResources([updatedCollection]));
77 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
78 dispatch(snackbarActions.OPEN_SNACKBAR({
79 message: 'New collection created.',
81 kind: SnackbarKind.SUCCESS
83 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
85 const error = getCommonResourceServiceError(e);
86 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
87 dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME, { name: 'Collection with this name already exists.' } as FormErrors));
88 } else if (error === CommonResourceServiceError.UNKNOWN) {
89 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
90 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
92 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
93 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
95 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
100 export const openCollectionPartialCopyToExistingCollectionDialog = () =>
101 (dispatch: Dispatch, getState: () => RootState) => {
102 const currentCollection = getState().collectionPanel.item;
103 if (currentCollection) {
104 const initialData = {
107 dispatch(initialize(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, initialData));
108 dispatch<any>(resetPickerProjectTree());
109 dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
110 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, data: {} }));
114 export const copyCollectionPartialToExistingCollection = ({ collectionUuid }: CollectionPartialCopyToExistingCollectionFormData) =>
115 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
116 const state = getState();
117 // Get current collection
118 const sourceCollection = state.collectionPanel.item;
120 if (sourceCollection) {
122 dispatch(startSubmit(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
123 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
124 // Get selected files
125 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
126 .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
129 const updatedCollection = await services.collectionService.copyFiles(sourceCollection.portableDataHash, paths, {uuid: collectionUuid}, '/', false);
130 dispatch(updateResources([updatedCollection]));
132 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
133 dispatch(snackbarActions.OPEN_SNACKBAR({
134 message: 'Files has been copied to selected collection.',
136 kind: SnackbarKind.SUCCESS
138 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
140 const error = getCommonResourceServiceError(e);
141 if (error === CommonResourceServiceError.UNKNOWN) {
142 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
143 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
145 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));