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 "~/services/common-service/common-resource-service";
11 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
12 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
13 import { resetPickerProjectTree } from '~/store/project-tree-picker/project-tree-picker-actions';
14 import { projectPanelActions } from '~/store/project-panel/project-panel-action';
15 import { getProcess, getProcessStatus, ProcessStatus } from '~/store/processes/process';
17 export const PROCESS_MOVE_FORM_NAME = 'processMoveFormName';
19 export const openMoveProcessDialog = (resource: { name: string, uuid: string }) =>
20 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
21 const process = getProcess(resource.uuid)(getState().resources);
23 const processStatus = getProcessStatus(process);
24 if (processStatus === ProcessStatus.DRAFT) {
25 dispatch<any>(resetPickerProjectTree());
26 dispatch(initialize(PROCESS_MOVE_FORM_NAME, resource));
27 dispatch(dialogActions.OPEN_DIALOG({ id: PROCESS_MOVE_FORM_NAME, data: {} }));
29 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'You can move only draft processes.', hideDuration: 2000 }));
32 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process not found', hideDuration: 2000 }));
36 export const moveProcess = (resource: MoveToFormDialogData) =>
37 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38 dispatch(startSubmit(PROCESS_MOVE_FORM_NAME));
40 const process = await services.containerRequestService.get(resource.uuid);
41 await services.containerRequestService.update(resource.uuid, { ...process, ownerUuid: resource.ownerUuid });
42 dispatch(projectPanelActions.REQUEST_ITEMS());
43 dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_MOVE_FORM_NAME }));
46 const error = getCommonResourceServiceError(e);
47 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
48 dispatch(stopSubmit(PROCESS_MOVE_FORM_NAME, { ownerUuid: 'A process with the same name already exists in the target project.' }));
49 } else if (error === CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE) {
50 dispatch(stopSubmit(PROCESS_MOVE_FORM_NAME, { ownerUuid: 'You can move only draft processes.' }));
52 dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_MOVE_FORM_NAME }));
53 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move the process.', hideDuration: 2000 }));