import { Grid } from '@mui/material'; import { FC, useMemo } from 'react'; import ActionGroup from '../ActionGroup'; import api from '../../lib/api'; import FormSummary from '../FormSummary'; import handleAPIError from '../../lib/handleAPIError'; import MessageGroup from '../MessageGroup'; import OutlinedInputWithLabel from '../OutlinedInputWithLabel'; import RadioGroupWithLabel from '../RadioGroupWithLabel'; import schema from './schema'; import UncontrolledInput from '../UncontrolledInput'; import useFormikUtils from '../../hooks/useFormikUtils'; const HOST_TYPE_OPTIONS: RadioItemList = { subnode: { label: 'Subnode', value: 'subnode' }, dr: { label: 'Disaster Recovery (DR) host', value: 'dr' }, }; const PrepareHostForm: FC = (props) => { const { host, tools } = props; const { disabledSubmit, formik, formikErrors, handleChange } = useFormikUtils({ initialValues: { ip: host.hostIpAddress, name: host.hostName, password: host.hostPassword, type: '', uuid: host.hostUUID, }, onSubmit: (values, { setSubmitting }) => { const { enterpriseKey, ip, name, password, type, uuid, redhatPassword, redhatUsername, } = values; tools.confirm.prepare({ actionProceedText: 'Prepare', content: , onCancelAppend: () => setSubmitting(false), onProceedAppend: () => { tools.confirm.loading(true); api .put('/host/prepare', { enterpriseUUID: enterpriseKey, hostIPAddress: ip, hostName: name, hostPassword: password, hostType: type === 'subnode' ? 'node' : type, hostUUID: uuid, redhatPassword, redhatUser: redhatUsername, }) .then(() => { tools.confirm.finish('Success', { children: <>Started job to prepare host at {ip}., }); tools.add.open(false); }) .catch((error) => { const emsg = handleAPIError(error); emsg.children = ( <> Failed to prepare host at {ip}. {emsg.children} ); tools.confirm.finish('Error', emsg); setSubmitting(false); }); }, titleText: `Prepare host at ${values.ip} with the following?`, }); tools.confirm.open(); }, validationSchema: schema, }); const enterpriseKeyChain = useMemo(() => 'enterpriseKey', []); const nameChain = useMemo(() => 'name', []); const redhatConfirmPasswordChain = useMemo( () => 'redhatConfirmPassword', [], ); const redhatPasswordChain = useMemo(() => 'redhatPassword', []); const redhatUsernameChain = useMemo(() => 'redhatUsername', []); const typeChain = useMemo(() => 'type', []); const showRedhatSection = useMemo( () => host.isInetConnected && /rhel/i.test(host.hostOS) && !host.isOSRegistered, [host.hostOS, host.isInetConnected, host.isOSRegistered], ); return ( { event.preventDefault(); formik.submitForm(); }} spacing="1em" > } /> } /> } /> {showRedhatSection && ( <> } /> } /> } /> )} ); }; export default PrepareHostForm;