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 { filterCollectionFilesBySelection } from "store/collection-panel/collection-panel-files/collection-panel-files-state";
10 import { dialogActions } from "store/dialog/dialog-actions";
11 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
12 import { resetPickerProjectTree } from "store/project-tree-picker/project-tree-picker-actions";
13 import { updateResources } from "store/resources/resources-actions";
14 import { SnackbarKind, snackbarActions } from "store/snackbar/snackbar-actions";
15 import { RootState } from "store/store";
16 import { initProjectsTreePicker } from "store/tree-picker/tree-picker-actions";
18 export const COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION_DIALOG';
19 export const COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION_DIALOG';
21 export interface CollectionPartialMoveToNewCollectionFormData {
27 export interface CollectionPartialMoveToExistingCollectionFormData {
28 collectionUuid: string;
31 export const openCollectionPartialMoveToNewCollectionDialog = () =>
32 (dispatch: Dispatch, getState: () => RootState) => {
33 const currentCollection = getState().collectionPanel.item;
34 if (currentCollection) {
36 name: `Files moved from: ${currentCollection.name}`,
37 description: currentCollection.description,
38 projectUuid: undefined
40 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, initialData));
41 dispatch<any>(resetPickerProjectTree());
42 dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
43 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION, data: {} }));
47 export const moveCollectionPartialToNewCollection = ({ name, description, projectUuid }: CollectionPartialMoveToNewCollectionFormData) =>
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_MOVE_TO_NEW_COLLECTION));
56 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
58 // Create new collection
59 const newCollection = await services.collectionService.create({
62 ownerUuid: projectUuid,
67 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
68 .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
71 const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, newCollection.uuid, '/', false);
72 dispatch(updateResources([updatedCollection]));
74 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
75 dispatch(snackbarActions.OPEN_SNACKBAR({
76 message: 'Files have been moved to selected collection.',
78 kind: SnackbarKind.SUCCESS
80 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
82 const error = getCommonResourceServiceError(e);
83 if (error === CommonResourceServiceError.UNKNOWN) {
84 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
85 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
87 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
92 export const openCollectionPartialMoveToExistingCollectionDialog = () =>
93 (dispatch: Dispatch, getState: () => RootState) => {
94 const currentCollection = getState().collectionPanel.item;
95 if (currentCollection) {
99 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialData));
100 dispatch<any>(resetPickerProjectTree());
101 dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
102 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: {} }));
106 export const moveCollectionPartialToExistingCollection = ({ collectionUuid }: CollectionPartialMoveToExistingCollectionFormData) =>
107 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
108 const state = getState();
109 // Get current collection
110 const sourceCollection = state.collectionPanel.item;
112 if (sourceCollection) {
114 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
115 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
116 // Get selected files
117 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
118 .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
121 const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, collectionUuid, '/', false);
122 dispatch(updateResources([updatedCollection]));
124 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
125 dispatch(snackbarActions.OPEN_SNACKBAR({
126 message: 'Files have been moved to selected collection.',
128 kind: SnackbarKind.SUCCESS
130 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
132 const error = getCommonResourceServiceError(e);
133 if (error === CommonResourceServiceError.UNKNOWN) {
134 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
135 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
137 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));