import { Grid } from '@mui/material'; import { FC, useCallback, useMemo, useRef, useState } from 'react'; import ConfirmDialog from './ConfirmDialog'; import ContainedButton from './ContainedButton'; import FlexBox from './FlexBox'; import GeneralInitForm, { GeneralInitFormForwardRefContent, GeneralInitFormValues, } from './GeneralInitForm'; import mainAxiosInstance from '../lib/singletons/mainAxiosInstance'; import MessageBox, { Message } from './MessageBox'; import NetworkInitForm, { NetworkInitFormForwardRefContent, NetworkInitFormValues, } from './NetworkInitForm'; import { Panel, PanelHeader } from './Panels'; import Spinner from './Spinner'; import { BodyText, HeaderText, InlineMonoText, MonoText } from './Text'; const StrikerInitForm: FC = () => { const [submitMessage, setSubmitMessage] = useState(); const [requestBody, setRequestBody] = useState< (GeneralInitFormValues & NetworkInitFormValues) | undefined >(); const [isOpenConfirm, setIsOpenConfirm] = useState(false); const [isDisableSubmit, setIsDisableSubmit] = useState(true); const [isGeneralInitFormValid, setIsGeneralInitFormValid] = useState(false); const [isNetworkInitFormValid, setIsNetworkInitFormValid] = useState(false); const [isSubmittingForm, setIsSubmittingForm] = useState(false); const generalInitFormRef = useRef({}); const networkInitFormRef = useRef({}); const buildSubmitSection = useMemo( () => isSubmittingForm ? ( ) : ( { setRequestBody({ ...(generalInitFormRef.current.get?.call(null) ?? {}), ...(networkInitFormRef.current.get?.call(null) ?? { networks: [], }), }); setIsOpenConfirm(true); }} > Initialize ), [isDisableSubmit, isSubmittingForm], ); const toggleSubmitDisabled = useCallback((...testResults: boolean[]) => { setIsDisableSubmit(!testResults.every((testResult) => testResult)); }, []); return ( <> { if (testResult !== isGeneralInitFormValid) { setIsGeneralInitFormValid(testResult); toggleSubmitDisabled(testResult, isNetworkInitFormValid); } }} /> { if (testResult !== isNetworkInitFormValid) { setIsNetworkInitFormValid(testResult); toggleSubmitDisabled(isGeneralInitFormValid, testResult); } }} /> {submitMessage && ( setSubmitMessage(undefined)} /> )} {buildSubmitSection} Organization name {requestBody?.organizationName} Organization prefix {requestBody?.organizationPrefix} Striker number {requestBody?.hostNumber} Domain name {requestBody?.domainName} Host name {requestBody?.hostName} Networks {requestBody?.networks.map( ({ inputUUID, interfaces, ipAddress, name, subnetMask, type, typeCount, }) => ( {name} ( {`${type.toUpperCase()}${typeCount}`} ) {interfaces.map((iface, interfaceIndex) => { if (iface === undefined) { return <>; } const { networkInterfaceName } = iface; return ( <> {`Link ${interfaceIndex + 1}`} {networkInterfaceName} ); })} {`${ipAddress}/${subnetMask}`} ), )} Gateway {requestBody?.gateway} Gateway network {requestBody?.gatewayInterface?.toUpperCase()} Domain name server(s) {requestBody?.domainNameServerCSV} } dialogProps={{ open: isOpenConfirm }} onCancel={() => { setIsOpenConfirm(false); }} onProceed={() => { setSubmitMessage(undefined); setIsSubmittingForm(true); setIsOpenConfirm(false); mainAxiosInstance .put('/command/initialize-striker', JSON.stringify(requestBody), { headers: { 'Content-Type': 'application/json' }, }) .then(() => { setIsSubmittingForm(false); }) .catch((reason) => { setSubmitMessage({ children: `Failed to submit; ${reason}`, type: 'error', }); setIsSubmittingForm(false); }); }} titleText="Confirm striker initialization" /> ); }; export default StrikerInitForm;