1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { dialogActions } from "~/store/dialog/dialog-actions";
7 import { startSubmit, stopSubmit, initialize } from 'redux-form';
8 import { ServiceRepository } from '~/services/services';
9 import { RootState } from '~/store/store';
10 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/common/api/common-resource-service";
11 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
12 import { projectPanelActions } from '~/store/project-panel/project-panel-action';
13 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
14 import { resetPickerProjectTree } from '~/store/project-tree-picker/project-tree-picker-actions';
16 export const COLLECTION_MOVE_FORM_NAME = 'collectionMoveFormName';
18 export const openMoveCollectionDialog = (resource: { name: string, uuid: string }) =>
19 (dispatch: Dispatch) => {
20 dispatch<any>(resetPickerProjectTree());
21 dispatch(initialize(COLLECTION_MOVE_FORM_NAME, resource));
22 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_MOVE_FORM_NAME, data: {} }));
25 export const moveCollection = (resource: MoveToFormDialogData) =>
26 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
27 dispatch(startSubmit(COLLECTION_MOVE_FORM_NAME));
29 const collection = await services.collectionService.get(resource.uuid);
30 await services.collectionService.update(resource.uuid, { ...collection, ownerUuid: resource.ownerUuid });
31 dispatch(projectPanelActions.REQUEST_ITEMS());
32 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_MOVE_FORM_NAME }));
33 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved', hideDuration: 2000 }));
35 const error = getCommonResourceServiceError(e);
36 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
37 dispatch(stopSubmit(COLLECTION_MOVE_FORM_NAME, { ownerUuid: 'A collection with the same name already exists in the target project.' }));
39 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_MOVE_FORM_NAME }));
40 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move the collection.', hideDuration: 2000 }));