21128: cleanup, renamed some types Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[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, FormErrors } 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, SnackbarKind } 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-bind";
15 import { getProcess } from "store/processes/process";
16 import { initProjectsTreePicker } from "store/tree-picker/tree-picker-actions";
17
18 export const PROCESS_MOVE_FORM_NAME = "processMoveFormName";
19
20 export const openMoveProcessDialog =
21     (resource: { name: string; uuid: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
22         const process = getProcess(resource.uuid)(getState().resources);
23         if (process) {
24             dispatch<any>(resetPickerProjectTree());
25             dispatch<any>(initProjectsTreePicker(PROCESS_MOVE_FORM_NAME));
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: "Process not found", hideDuration: 2000, kind: SnackbarKind.ERROR }));
30         }
31     };
32
33 export const moveProcess = (resource: MoveToFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34     dispatch(startSubmit(PROCESS_MOVE_FORM_NAME));
35     try {
36         const process = await services.containerRequestService.get(resource.uuid);
37         await services.containerRequestService.update(resource.uuid, { ownerUuid: resource.ownerUuid });
38         dispatch(projectPanelActions.REQUEST_ITEMS());
39         dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_MOVE_FORM_NAME }));
40         return process;
41     } catch (e) {
42         const error = getCommonResourceServiceError(e);
43         if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
44             dispatch(
45                 stopSubmit(PROCESS_MOVE_FORM_NAME, { ownerUuid: "A process with the same name already exists in the target project." } as FormErrors)
46             );
47         } else {
48             dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_MOVE_FORM_NAME }));
49             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Could not move the process.", hideDuration: 2000, kind: SnackbarKind.ERROR }));
50         }
51         return;
52     }
53 };