import { FC, useCallback, useMemo, useRef, useState } from 'react'; import ContainedButton from './ContainedButton'; import FlexBox from './FlexBox'; import GeneralInitForm, { GeneralInitFormForwardRefContent, } from './GeneralInitForm'; import mainAxiosInstance from '../lib/singletons/mainAxiosInstance'; import MessageBox, { Message } from './MessageBox'; import NetworkInitForm, { NetworkInitFormForwardRefContent, } from './NetworkInitForm'; import { Panel, PanelHeader } from './Panels'; import Spinner from './Spinner'; import { HeaderText } from './Text'; const StrikerInitForm: FC = () => { const [submitMessage, setSubmitMessage] = useState(); 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 ? ( ) : ( { setIsSubmittingForm(true); const requestBody: string = JSON.stringify({ ...(generalInitFormRef.current.get?.call(null) ?? {}), ...(networkInitFormRef.current.get?.call(null) ?? {}), }); mainAxiosInstance .put('/command/initialize-striker', requestBody, { headers: { 'Content-Type': 'application/json' }, }) .then(() => { setIsSubmittingForm(false); }) .catch((reason) => { setSubmitMessage({ children: `Failed to submit; ${reason}`, type: 'error', }); setIsSubmittingForm(false); }); }} > 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} ); }; export default StrikerInitForm;