fix(striker-ui): correct requests in striker init components

main
Tsu-ba-me 2 years ago
parent d64ad303e8
commit cefbba36ca
  1. 47
      striker-ui/components/NetworkInitForm.tsx
  2. 2
      striker-ui/components/StrikerInitForm.tsx

@ -37,10 +37,12 @@ import { BLUE, GREY } from '../lib/consts/DEFAULT_THEME';
import NETWORK_TYPES from '../lib/consts/NETWORK_TYPES'; import NETWORK_TYPES from '../lib/consts/NETWORK_TYPES';
import { REP_IPV4, REP_IPV4_CSV } from '../lib/consts/REG_EXP_PATTERNS'; import { REP_IPV4, REP_IPV4_CSV } from '../lib/consts/REG_EXP_PATTERNS';
import api from '../lib/api';
import BriefNetworkInterface from './BriefNetworkInterface'; import BriefNetworkInterface from './BriefNetworkInterface';
import Decorator from './Decorator'; import Decorator from './Decorator';
import DropArea from './DropArea'; import DropArea from './DropArea';
import FlexBox from './FlexBox'; import FlexBox from './FlexBox';
import handleAPIError from '../lib/handleAPIError';
import IconButton from './IconButton'; import IconButton from './IconButton';
import InputWithRef, { InputForwardedRefContent } from './InputWithRef'; import InputWithRef, { InputForwardedRefContent } from './InputWithRef';
import { Message } from './MessageBox'; import { Message } from './MessageBox';
@ -106,6 +108,7 @@ const CLASSES = {
ifaceNotApplied: `${CLASS_PREFIX}-network-interface-not-applied`, ifaceNotApplied: `${CLASS_PREFIX}-network-interface-not-applied`,
}; };
const INITIAL_IFACES = [undefined, undefined]; const INITIAL_IFACES = [undefined, undefined];
const MSG_ID_API = 'api';
const STRIKER_REQUIRED_NETWORKS: NetworkInput[] = [ const STRIKER_REQUIRED_NETWORKS: NetworkInput[] = [
{ {
@ -605,7 +608,7 @@ const NetworkInitForm = forwardRef<
const { data: networkInterfaces = [], isLoading } = periodicFetch< const { data: networkInterfaces = [], isLoading } = periodicFetch<
NetworkInterfaceOverviewMetadata[] NetworkInterfaceOverviewMetadata[]
>(`${API_BASE_URL}/network-interface/${hostUUID}`, { >(`${API_BASE_URL}/init/network-interface/${hostUUID}`, {
refreshInterval: 2000, refreshInterval: 2000,
onSuccess: (data) => { onSuccess: (data) => {
const map = data.reduce<NetworkInterfaceInputMap>((result, metadata) => { const map = data.reduce<NetworkInterfaceInputMap>((result, metadata) => {
@ -716,6 +719,24 @@ const NetworkInitForm = forwardRef<
[networkInputs], [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);
});
},
[setMessage],
);
const inputTests: InputTestBatches = useMemo(() => { const inputTests: InputTestBatches = useMemo(() => {
const tests: InputTestBatches = { const tests: InputTestBatches = {
[IT_IDS.dnsCSV]: { [IT_IDS.dnsCSV]: {
@ -1117,6 +1138,30 @@ const NetworkInitForm = forwardRef<
[clearNetworkInterfaceHeld, networkInterfaceHeld], [clearNetworkInterfaceHeld, networkInterfaceHeld],
); );
useEffect(() => {
// Enable network mapping on component mount.
setMapNetwork(1);
if (window) {
window.addEventListener(
'beforeunload',
() => {
// Cannot use async request (i.e., axios) because they won't be guaranteed to complete.
const request = new XMLHttpRequest();
request.open('PUT', `${API_BASE_URL}/init/set-map-network`, false);
request.send(null);
},
{ once: true },
);
}
return () => {
// Disable network mapping on component unmount.
setMapNetwork(0);
};
}, [setMapNetwork]);
useImperativeHandle( useImperativeHandle(
ref, ref,
() => ({ () => ({

@ -220,7 +220,7 @@ const StrikerInitForm: FC = () => {
setIsOpenConfirm(false); setIsOpenConfirm(false);
api api
.post('/host', requestBody) .post('/init', requestBody)
.then(() => { .then(() => {
setIsSubmittingForm(false); setIsSubmittingForm(false);
}) })

Loading…
Cancel
Save