import { FC, useCallback, useMemo, useRef, useState } from 'react'; import { Grid } from '@mui/material'; import ActionGroup from '../ActionGroup'; import api from '../../lib/api'; import handleAPIError from '../../lib/handleAPIError'; import MessageGroup, { MessageGroupForwardedRefContent } from '../MessageGroup'; import OutlinedInputWithLabel from '../OutlinedInputWithLabel'; import UncontrolledInput from '../UncontrolledInput'; import useFormikUtils from '../../hooks/useFormikUtils'; import Spinner from '../Spinner'; import schema from './testAccessSchema'; const TestAccessForm: FC = (props) => { const { setResponse } = props; const messageGroupRef = useRef(null); const [loadingInquiry, setLoadingInquiry] = useState(false); const setApiMessage = useCallback( (message?: Message) => messageGroupRef?.current?.setMessage?.call(null, 'api', message), [], ); const { disabledSubmit, formik, formikErrors, handleChange } = useFormikUtils({ initialValues: { ip: '', password: '', }, onSubmit: (values, { setSubmitting }) => { setApiMessage(); setLoadingInquiry(true); setResponse(undefined); const { ip, password } = values; api .put('/command/inquire-host', { ipAddress: ip, password, }) .then(({ data }) => { const { isConnected } = data; if (!isConnected) { setApiMessage({ children: ( <> Failed to connect. Please make sure the credentials are correct, and the host is reachable from this striker. ), type: 'warning', }); return; } setResponse({ ...data, hostIpAddress: ip, hostPassword: password, }); setApiMessage(); }) .catch((error) => { const emsg = handleAPIError(error); emsg.children = ( <> Failed to access {ip}. {emsg.children} ); setApiMessage(emsg); }) .finally(() => { setSubmitting(false); setLoadingInquiry(false); }); }, validationSchema: schema, }); const ipChain = useMemo(() => 'ip', []); const passwordChain = useMemo(() => 'password', []); return ( { event.preventDefault(); formik.submitForm(); }} spacing="1em" > } /> } /> {loadingInquiry ? ( ) : ( )} ); }; export default TestAccessForm;