import { FormikErrors } from 'formik'; import { ChangeEvent } from 'react'; import { Plus, Trash2 } from 'lucide-react'; import { InputGroup } from '@@/form-components/InputGroup'; import { FormError } from '@@/form-components/FormError'; import { ButtonSelector } from '@@/form-components/ButtonSelector/ButtonSelector'; import { Button } from '@@/buttons'; import { isServicePortError, newPort } from './utils'; import { ContainerPortInput } from './ContainerPortInput'; import { ServicePortInput } from './ServicePortInput'; import { ServicePort } from './types'; interface Props { values: ServicePort[]; onChange: (loadBalancerPorts: ServicePort[]) => void; loadBalancerEnabled: boolean; serviceName?: string; errors?: string | string[] | FormikErrors[]; } export function LoadBalancerForm({ values: loadBalancerPorts, onChange, loadBalancerEnabled, serviceName, errors, }: Props) { const newLoadBalancerPort = newPort(serviceName); return ( <> {loadBalancerEnabled && ( <>
Published ports
{loadBalancerPorts.map((lbPort, index) => { const error = errors?.[index]; const servicePortError = isServicePortError(error) ? error : undefined; return (
) => { const newServicePorts = [...loadBalancerPorts]; const newValue = e.target.value === '' ? undefined : Number(e.target.value); newServicePorts[index] = { ...newServicePorts[index], targetPort: newValue, port: newValue, }; onChange(newServicePorts); }} /> {servicePortError?.targetPort && ( {servicePortError.targetPort} )}
) => { const newServicePorts = [...loadBalancerPorts]; newServicePorts[index] = { ...newServicePorts[index], port: e.target.value === '' ? undefined : Number(e.target.value), }; onChange(newServicePorts); }} /> {servicePortError?.port && ( {servicePortError.port} )}
Loadbalancer port ) => { const newServicePorts = [...loadBalancerPorts]; newServicePorts[index] = { ...newServicePorts[index], port: e.target.value === '' ? undefined : Number(e.target.value), }; onChange(newServicePorts); }} required data-cy={`k8sAppCreate-loadbalancerPort_${index}`} /> {servicePortError?.nodePort && ( {servicePortError.nodePort} )}
{ const newServicePorts = [...loadBalancerPorts]; newServicePorts[index] = { ...newServicePorts[index], protocol: value, }; onChange(newServicePorts); }} value={lbPort.protocol || 'TCP'} options={[{ value: 'TCP' }, { value: 'UDP' }]} />
); })}
)} ); }