Merge branch '14433_properties_inside_projects'
[arvados-workbench2.git] / src / store / processes / process-copy-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 { initialize, startSubmit } from 'redux-form';
8 import { resetPickerProjectTree } from '~/store/project-tree-picker/project-tree-picker-actions';
9 import { RootState } from '~/store/store';
10 import { ServiceRepository } from '~/services/services';
11 import { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
12 import { getProcess, ProcessStatus, getProcessStatus } from '~/store/processes/process';
13 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
14 import { initProjectsTreePicker } from '~/store/tree-picker/tree-picker-actions';
15
16 export const PROCESS_COPY_FORM_NAME = 'processCopyFormName';
17
18 export const openCopyProcessDialog = (resource: { name: string, uuid: string }) =>
19     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
20         const process = getProcess(resource.uuid)(getState().resources);
21         if (process) {
22             const processStatus = getProcessStatus(process);
23             if (processStatus === ProcessStatus.DRAFT) {
24                 dispatch<any>(resetPickerProjectTree());
25                 dispatch<any>(initProjectsTreePicker(PROCESS_COPY_FORM_NAME));
26                 const initialData: CopyFormDialogData = { name: `Copy of: ${resource.name}`, uuid: resource.uuid, ownerUuid: '' };
27                 dispatch<any>(initialize(PROCESS_COPY_FORM_NAME, initialData));
28                 dispatch(dialogActions.OPEN_DIALOG({ id: PROCESS_COPY_FORM_NAME, data: {} }));
29             } else {
30                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'You can copy only draft processes.', hideDuration: 2000 }));
31             }
32         } else {
33             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process not found', hideDuration: 2000 }));
34         }
35     };
36
37 export const copyProcess = (resource: CopyFormDialogData) =>
38     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39         dispatch(startSubmit(PROCESS_COPY_FORM_NAME));
40         try {
41             const process = await services.containerRequestService.get(resource.uuid);
42             const uuidKey = 'uuid';
43             delete process[uuidKey];
44             await services.containerRequestService.create({ ...process, ownerUuid: resource.ownerUuid, name: resource.name });
45             dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_COPY_FORM_NAME }));
46             return process;
47         } catch (e) {
48             dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_COPY_FORM_NAME }));
49             throw new Error('Could not copy the process.');
50         }
51     };