From cb395fe1ac915c5fe8cd308786e79dfdfd5e1b5a Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Mon, 27 Nov 2023 10:47:33 -0500 Subject: [PATCH] fix(striker-ui): disable network mapping on successful submission --- striker-ui/components/NetworkInitForm.tsx | 26 ++++++-------------- striker-ui/components/PrepareNetworkForm.tsx | 4 +++ striker-ui/components/StrikerInitForm.tsx | 4 +++ striker-ui/lib/setMapNetwork.tsx | 22 +++++++++++++++++ 4 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 striker-ui/lib/setMapNetwork.tsx diff --git a/striker-ui/components/NetworkInitForm.tsx b/striker-ui/components/NetworkInitForm.tsx index c94e8c1a..83ee5706 100644 --- a/striker-ui/components/NetworkInitForm.tsx +++ b/striker-ui/components/NetworkInitForm.tsx @@ -37,12 +37,10 @@ import { BLUE, GREY } from '../lib/consts/DEFAULT_THEME'; import NETWORK_TYPES from '../lib/consts/NETWORK_TYPES'; import { REP_IPV4, REP_IPV4_CSV } from '../lib/consts/REG_EXP_PATTERNS'; -import api from '../lib/api'; import BriefNetworkInterface from './BriefNetworkInterface'; import Decorator from './Decorator'; import DropArea from './DropArea'; import FlexBox from './FlexBox'; -import handleAPIError from '../lib/handleAPIError'; import IconButton from './IconButton'; import InputWithRef, { InputForwardedRefContent } from './InputWithRef'; import { Message } from './MessageBox'; @@ -51,6 +49,7 @@ import OutlinedInputWithLabel from './OutlinedInputWithLabel'; import { InnerPanel, InnerPanelHeader } from './Panels'; import periodicFetch from '../lib/fetchers/periodicFetch'; import SelectWithLabel from './SelectWithLabel'; +import setMapNetwork from '../lib/setMapNetwork'; import Spinner from './Spinner'; import { createTestInputFunction, testNotBlank } from '../lib/test_input'; import { BodyText, MonoText, SmallText } from './Text'; @@ -873,20 +872,9 @@ const NetworkInitForm = forwardRef< [networkInputs], ); - const setMapNetwork = useCallback( - (value: 0 | 1) => { - api.put('/init/set-map-network', { value }).catch((error) => { - const emsg = handleAPIError(error); - - emsg.children = ( - <> - Failed to {value ? 'enable' : 'disable'} network mapping.{' '} - {emsg.children} - - ); - - setMessage(MSG_ID_API, emsg); - }); + const handleSetMapNetworkError = useCallback( + (msg: Message): void => { + setMessage(MSG_ID_API, msg); }, [setMessage], ); @@ -1383,7 +1371,7 @@ const NetworkInitForm = forwardRef< useEffect(() => { // Enable network mapping on component mount. - setMapNetwork(1); + setMapNetwork(1, handleSetMapNetworkError); if (window) { window.addEventListener( @@ -1401,9 +1389,9 @@ const NetworkInitForm = forwardRef< return () => { // Disable network mapping on component unmount. - setMapNetwork(0); + setMapNetwork(0, handleSetMapNetworkError); }; - }, [setMapNetwork]); + }, [handleSetMapNetworkError]); useImperativeHandle( ref, diff --git a/striker-ui/components/PrepareNetworkForm.tsx b/striker-ui/components/PrepareNetworkForm.tsx index 5e0ea4db..a95fdbd0 100644 --- a/striker-ui/components/PrepareNetworkForm.tsx +++ b/striker-ui/components/PrepareNetworkForm.tsx @@ -16,6 +16,7 @@ import NetworkInitForm, { } from './NetworkInitForm'; import OutlinedInputWithLabel from './OutlinedInputWithLabel'; import { Panel, PanelHeader } from './Panels'; +import setMapNetwork from '../lib/setMapNetwork'; import Spinner from './Spinner'; import { buildPeacefulStringTestBatch } from '../lib/test_input'; import { HeaderText } from './Text'; @@ -212,6 +213,9 @@ const PrepareNetworkForm: FC = ({ <>Failed to prepare network. {parentMsg} ), method: 'put', + onSuccess: () => { + setMapNetwork(0); + }, setMsg: netconfFormRef?.current?.setMessage, successMsg: `Initiated prepare network on ${hostDetail?.shortHostName}`, url: `/host/${hostUUID}?handler=subnode-network`, diff --git a/striker-ui/components/StrikerInitForm.tsx b/striker-ui/components/StrikerInitForm.tsx index 83fdfb81..2fd7c1d7 100644 --- a/striker-ui/components/StrikerInitForm.tsx +++ b/striker-ui/components/StrikerInitForm.tsx @@ -27,6 +27,7 @@ import NetworkInitForm, { NetworkInitFormValues, } from './NetworkInitForm'; import { Panel, PanelHeader } from './Panels'; +import setMapNetwork from '../lib/setMapNetwork'; import Spinner from './Spinner'; import { BodyText, HeaderText, InlineMonoText, MonoText } from './Text'; import useProtectedState from '../hooks/useProtectedState'; @@ -303,6 +304,9 @@ const StrikerInitForm: FC = () => { api .put('/init', requestBody) .then(() => { + // Stop network mapping only on successful form submission. + setMapNetwork(0); + setIsSubmittingForm(false); setSubmitMessage({ children: ( diff --git a/striker-ui/lib/setMapNetwork.tsx b/striker-ui/lib/setMapNetwork.tsx new file mode 100644 index 00000000..2a190cbd --- /dev/null +++ b/striker-ui/lib/setMapNetwork.tsx @@ -0,0 +1,22 @@ +import api from './api'; +import handleAPIError from './handleAPIError'; + +const setMapNetwork = ( + value: 0 | 1, + handleError?: (msg: Message) => void, +): void => { + api.put('/init/set-map-network', { value }).catch((error) => { + const emsg = handleAPIError(error); + + emsg.children = ( + <> + Failed to {value ? 'enable' : 'disable'} network mapping.{' '} + {emsg.children} + + ); + + handleError?.call(null, emsg); + }); +}; + +export default setMapNetwork;