import { Form } from 'formik'; import { get, map, without } from 'lodash'; import React from 'react'; import { Modal } from 'react-bootstrap'; import type { Application } from '../../../../application'; import type { IPipeline } from '../../../../domain'; import { ModalClose } from '../../../../modal'; import type { IModalComponentProps, IValidator } from '../../../../presentation'; import { FormikFormField, SpinFormik, TextInput, Validators } from '../../../../presentation'; import { PipelineConfigService } from '../../services/PipelineConfigService'; export interface IRenamePipelineModalProps extends IModalComponentProps { application: Application; pipeline: IPipeline; } interface IRenamePipelineCommand { name: string; } export function RenamePipelineModal(props: IRenamePipelineModalProps) { const { application, closeModal, dismissModal, pipeline } = props; const [errorMessage, setErrorMessage] = React.useState(null); const [saveError, setSaveError] = React.useState(false); const [saving, setSaving] = React.useState(false); const existingNames = without(map(application.pipelineConfigs.data, 'name'), pipeline.name); const pipelineType = pipeline.strategy === true ? 'Strategy' : 'Pipeline'; const initialValues: IRenamePipelineCommand = { name: pipeline.name }; const validPipelineName = (): IValidator => { return (val: string) => { const message = `${pipelineType} cannot contain: \\ ^ / ? % #`; return val && !/^[^\\^/?%#]*$/i.test(val) && message; }; }; function renamePipeline(command: IRenamePipelineCommand) { setSaving(true); PipelineConfigService.renamePipeline(application.name, pipeline, pipeline.name, command.name).then( () => { application.pipelineConfigs.refresh(); closeModal(command.name); }, (response) => { setSaving(false); setSaveError(true); setErrorMessage(get(response, 'data.message', 'No message provided')); }, ); } return ( <> initialValues={initialValues} onSubmit={renamePipeline} render={(formik) => (
{}}> Rename Pipeline {saveError && (

Could not rename pipeline.

Reason: {errorMessage}

{ e.preventDefault(); setSaveError(false); }} > [dismiss]

)} } required={true} validate={[ Validators.valueUnique(existingNames, `There is already a ${pipelineType} with that name.`), validPipelineName(), ]} />
)} /> ); }