1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { FormErrors, 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 { projectPanelDataActions } from "store/project-panel/project-panel-action-bind";
13 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
15 export interface ProcessUpdateFormDialogData {
21 export const PROCESS_UPDATE_FORM_NAME = "processUpdateFormName";
23 export const openProcessUpdateDialog =
24 (resource: ProcessUpdateFormDialogData) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
25 const process = getProcess(resource.uuid)(getState().resources);
27 dispatch(initialize(PROCESS_UPDATE_FORM_NAME, { ...resource, name: process.containerRequest.name }));
28 dispatch(dialogActions.OPEN_DIALOG({ id: PROCESS_UPDATE_FORM_NAME, data: {} }));
30 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Process not found", hideDuration: 2000, kind: SnackbarKind.ERROR }));
34 export const updateProcess =
35 (resource: ProcessUpdateFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36 dispatch(startSubmit(PROCESS_UPDATE_FORM_NAME));
38 const updatedProcess = await services.containerRequestService.update(resource.uuid, {
40 description: resource.description,
42 dispatch(projectPanelDataActions.REQUEST_ITEMS());
43 dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_UPDATE_FORM_NAME }));
44 return updatedProcess;
46 const error = getCommonResourceServiceError(e);
47 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
48 dispatch(stopSubmit(PROCESS_UPDATE_FORM_NAME, { name: "Process with the same name already exists." } as FormErrors));
50 dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_UPDATE_FORM_NAME }));
51 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Could not update the process.", hideDuration: 2000, kind: SnackbarKind.ERROR }));