Merge branch 'master'
[arvados-workbench2.git] / src / store / processes / process-update-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 { initialize, startSubmit, stopSubmit } from 'redux-form';
7 import { RootState } from "~/store/store";
8 import { dialogActions } from "~/store/dialog/dialog-actions";
9 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/services/common-service/common-resource-service";
10 import { ServiceRepository } from "~/services/services";
11 import { getProcess } from '~/store/processes/process';
12 import { projectPanelActions } from '~/store/project-panel/project-panel-action';
13 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
14
15 export interface ProcessUpdateFormDialogData {
16     uuid: string;
17     name: string;
18 }
19
20 export const PROCESS_UPDATE_FORM_NAME = 'processUpdateFormName';
21
22 export const openProcessUpdateDialog = (resource: ProcessUpdateFormDialogData) =>
23     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
24         const process = getProcess(resource.uuid)(getState().resources);
25         if(process) {
26             dispatch(initialize(PROCESS_UPDATE_FORM_NAME, { ...resource, name: process.containerRequest.name }));
27             dispatch(dialogActions.OPEN_DIALOG({ id: PROCESS_UPDATE_FORM_NAME, data: {} }));
28         } else {
29             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process not found', hideDuration: 2000 }));
30         }
31     };
32
33 export const updateProcess = (resource: ProcessUpdateFormDialogData) =>
34     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35         dispatch(startSubmit(PROCESS_UPDATE_FORM_NAME));
36         try {
37             const process = await services.containerRequestService.get(resource.uuid);
38             const updatedProcess = await services.containerRequestService.update(resource.uuid, { ...process, name: resource.name });
39             dispatch(projectPanelActions.REQUEST_ITEMS());
40             dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_UPDATE_FORM_NAME }));
41             return updatedProcess;
42         } catch (e) {
43             const error = getCommonResourceServiceError(e);
44             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
45                 dispatch(stopSubmit(PROCESS_UPDATE_FORM_NAME, { name: 'Process with the same name already exists.' }));
46             } else if (error === CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE) {
47                 dispatch(stopSubmit(PROCESS_UPDATE_FORM_NAME, { name: 'You cannot modified in "Final" state.' }));
48             } else {
49                 dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_UPDATE_FORM_NAME }));
50                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not update the process.', hideDuration: 2000 }));
51             }
52             return;
53         }
54     };