Merge branch 'master'
[arvados.git] / src / store / processes / process-move-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
16
17 export const PROCESS_MOVE_FORM_NAME = 'processMoveFormName';
18
19 export const openMoveProcessDialog = (resource: { name: string, uuid: string }) =>
20     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
21         const process = getProcess(resource.uuid)(getState().resources);
22         if (process) {
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: {} }));
28             } else {
29                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'You can move only draft processes.', hideDuration: 2000 }));
30             }
31         } else {
32             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process not found', hideDuration: 2000 }));
33         }
34     };
35
36 export const moveProcess = (resource: MoveToFormDialogData) =>
37     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38         dispatch(startSubmit(PROCESS_MOVE_FORM_NAME));
39         try {
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 }));
44             return process;
45         } catch (e) {
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.' }));
51             } else {
52                 dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_MOVE_FORM_NAME }));
53                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move the process.', hideDuration: 2000 }));
54             }
55             return;
56         }
57     };