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));
59 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
60 .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
63 const updatedCollection = await services.collectionService.moveFiles(
64 sourceCollection.uuid,
65 sourceCollection.portableDataHash,
70 ownerUuid: projectUuid,
76 dispatch(updateResources([updatedCollection]));
78 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
79 dispatch(snackbarActions.OPEN_SNACKBAR({
80 message: 'Files have been moved to selected collection.',
82 kind: SnackbarKind.SUCCESS
84 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
86 const error = getCommonResourceServiceError(e);
87 if (error === CommonResourceServiceError.UNKNOWN) {
88 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
89 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
91 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
96 export const openCollectionPartialMoveToExistingCollectionDialog = () =>
97 (dispatch: Dispatch, getState: () => RootState) => {
98 const currentCollection = getState().collectionPanel.item;
99 if (currentCollection) {
100 const initialData = {
103 dispatch(initialize(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, initialData));
104 dispatch<any>(resetPickerProjectTree());
105 dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
106 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION, data: {} }));
110 export const moveCollectionPartialToExistingCollection = ({ collectionUuid }: CollectionPartialMoveToExistingCollectionFormData) =>
111 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
112 const state = getState();
113 // Get current collection
114 const sourceCollection = state.collectionPanel.item;
116 if (sourceCollection) {
118 dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
119 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
120 // Get selected files
121 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
122 .map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
125 const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, {uuid: collectionUuid}, '/', false);
126 dispatch(updateResources([updatedCollection]));
128 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
129 dispatch(snackbarActions.OPEN_SNACKBAR({
130 message: 'Files have been moved to selected collection.',
132 kind: SnackbarKind.SUCCESS
134 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));
136 const error = getCommonResourceServiceError(e);
137 if (error === CommonResourceServiceError.UNKNOWN) {
138 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));
139 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
141 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION));