setting-breadcrumbs
[arvados-workbench2.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';
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 = (resource: { name: string, uuid: string }) =>
21     (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) =>
34     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35         dispatch(startSubmit(PROCESS_MOVE_FORM_NAME));
36         try {
37             const process = await services.containerRequestService.get(resource.uuid);
38             await services.containerRequestService.update(resource.uuid, { ownerUuid: resource.ownerUuid });
39             dispatch(projectPanelActions.REQUEST_ITEMS());
40             dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_MOVE_FORM_NAME }));
41             return process;
42         } catch (e) {
43             const error = getCommonResourceServiceError(e);
44             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
45                 dispatch(stopSubmit(PROCESS_MOVE_FORM_NAME, { ownerUuid: 'A process with the same name already exists in the target project.' } as FormErrors));
46             } else {
47                 dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_MOVE_FORM_NAME }));
48                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move the process.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
49             }
50             return;
51         }
52     };