diff --git a/striker-ui/components/ManageManifest/AddManifestInputGroup.tsx b/striker-ui/components/ManageManifest/AddManifestInputGroup.tsx index c5220fad..7617f204 100644 --- a/striker-ui/components/ManageManifest/AddManifestInputGroup.tsx +++ b/striker-ui/components/ManageManifest/AddManifestInputGroup.tsx @@ -12,7 +12,7 @@ import AnvilNetworkConfigInputGroup, { } from './AnvilNetworkConfigInputGroup'; import FlexBox from '../FlexBox'; -const DEFAULT_NETWORKS: AnvilNetworkConfigNetworkList = { +const DEFAULT_NETWORKS: ManifestNetworkList = { bcn1: { networkMinIp: '', networkNumber: 1, @@ -51,7 +51,7 @@ const AddManifestInputGroup = < previousNetworkConfig; const [networkList, setNetworkList] = - useState(previousNetworkList); + useState(previousNetworkList); return ( diff --git a/striker-ui/components/ManageManifest/AnvilNetworkConfigInputGroup.tsx b/striker-ui/components/ManageManifest/AnvilNetworkConfigInputGroup.tsx index 8c4719f5..332276f8 100644 --- a/striker-ui/components/ManageManifest/AnvilNetworkConfigInputGroup.tsx +++ b/striker-ui/components/ManageManifest/AnvilNetworkConfigInputGroup.tsx @@ -63,7 +63,7 @@ const AnvilNetworkConfigInputGroup = < input = networkListEntries, end = networkListEntries.length, }: { - input?: Array<[string, AnvilNetworkConfigNetwork]>; + input?: Array<[string, ManifestNetwork]>; end?: number; } = {}, ) => { @@ -101,8 +101,8 @@ const AnvilNetworkConfigInputGroup = < // Params that depend on others. networkGateway = assertIfn(networkType) ? '' : undefined, networkNumber = getNetworkNumber(networkType) + 1, - }: Partial = {}): { - network: AnvilNetworkConfigNetwork; + }: Partial = {}): { + network: ManifestNetwork; networkId: string; } => ({ network: { @@ -118,72 +118,73 @@ const AnvilNetworkConfigInputGroup = < ); const setNetwork = useCallback( - (key: string, value?: AnvilNetworkConfigNetwork) => + (key: string, value?: ManifestNetwork) => setNetworkList(buildObjectStateSetterCallback(key, value)), [setNetworkList], ); - const handleNetworkTypeChange = useCallback( - ( - { networkId: targetId, networkType: previousType }, - { target: { value } }, - ) => { - const newType = String(value); - - let isIdMatch = false; - let newTypeNumber = 0; + const handleNetworkTypeChange = + useCallback( + ( + { networkId: targetId, networkType: previousType }, + { target: { value } }, + ) => { + const newType = String(value); - const newList = networkListEntries.reduce( - (previous, [networkId, networkValue]) => { - const { networkNumber: initnn, networkType: initnt } = networkValue; + let isIdMatch = false; + let newTypeNumber = 0; - let networkNumber = initnn; - let networkType = initnt; + const newList = networkListEntries.reduce( + (previous, [networkId, networkValue]) => { + const { networkNumber: initnn, networkType: initnt } = networkValue; - if (networkId === targetId) { - isIdMatch = true; + let networkNumber = initnn; + let networkType = initnt; - networkType = newType; - } + if (networkId === targetId) { + isIdMatch = true; - const isTypeMatch = networkType === newType; + networkType = newType; + } - if (isTypeMatch) { - newTypeNumber += 1; - } + const isTypeMatch = networkType === newType; - if (isIdMatch) { if (isTypeMatch) { - networkNumber = newTypeNumber; - } else if (networkType === previousType) { - networkNumber -= 1; + newTypeNumber += 1; } - previous[networkId] = { - ...networkValue, - networkNumber, - networkType, - }; - } else { - previous[networkId] = networkValue; - } + if (isIdMatch) { + if (isTypeMatch) { + networkNumber = newTypeNumber; + } else if (networkType === previousType) { + networkNumber -= 1; + } - return previous; - }, - {}, - ); + previous[networkId] = { + ...networkValue, + networkNumber, + networkType, + }; + } else { + previous[networkId] = networkValue; + } - setNetworkList(newList); - }, - [networkListEntries, setNetworkList], - ); + return previous; + }, + {}, + ); + + setNetworkList(newList); + }, + [networkListEntries, setNetworkList], + ); - const handleNetworkRemove = useCallback( + const handleNetworkRemove = useCallback( ({ networkId: rmId, networkType: rmType }) => { let isIdMatch = false; let networkNumber = 0; - const newList = networkListEntries.reduce( + const newList = networkListEntries.reduce( (previous, [networkId, networkValue]) => { if (networkId === rmId) { isIdMatch = true; diff --git a/striker-ui/types/ManageManifest.d.ts b/striker-ui/types/ManageManifest.d.ts index e53dbbe4..aac4dd01 100644 --- a/striker-ui/types/ManageManifest.d.ts +++ b/striker-ui/types/ManageManifest.d.ts @@ -11,7 +11,7 @@ type AnvilIdInputGroupProps = formUtils: FormUtils; }; -type AnvilNetworkConfigNetwork = { +type ManifestNetwork = { networkGateway?: string; networkMinIp: string; networkNumber: number; @@ -19,17 +19,21 @@ type AnvilNetworkConfigNetwork = { networkType: string; }; -type AnvilNetworkConfigNetworkList = { - [networkId: string]: AnvilNetworkConfigNetwork; +type ManifestNetworkList = { + [networkId: string]: ManifestNetwork; }; -type AnvilNetworkCloseHandler = ( - args: { networkId: string } & Pick, +type AnvilNetworkEventHandlerPreviousArgs = { + networkId: string; +} & Pick; + +type AnvilNetworkCloseEventHandler = ( + args: AnvilNetworkEventHandlerPreviousArgs, ...handlerArgs: Parameters ) => ReturnType; -type AnvilNetworkTypeChangeHandler = ( - args: { networkId: string } & Pick, +type AnvilNetworkTypeChangeEventHandler = ( + args: AnvilNetworkEventHandlerPreviousArgs, ...handlerArgs: Parameters ) => ReturnType; @@ -38,8 +42,8 @@ type AnvilNetworkInputGroupOptionalProps = { inputGatewayLabel?: string; inputMinIpLabel?: string; inputSubnetMaskLabel?: string; - onClose?: AnvilNetworkCloseHandler; - onNetworkTypeChange?: AnvilNetworkTypeChangeHandler; + onClose?: AnvilNetworkCloseEventHandler; + onNetworkTypeChange?: AnvilNetworkTypeChangeEventHandler; previous?: { gateway?: string; minIp?: string; @@ -99,7 +103,7 @@ type AnvilNetworkConfigInputGroupOptionalProps = { dnsCsv?: string; /** Max Transmission Unit (MTU); unit: bytes */ mtu?: number; - networks?: AnvilNetworkConfigNetworkList; + networks?: ManifestNetworkList; ntpCsv?: string; }; }; @@ -107,9 +111,9 @@ type AnvilNetworkConfigInputGroupOptionalProps = { type AnvilNetworkConfigInputGroupProps = AnvilNetworkConfigInputGroupOptionalProps & { formUtils: FormUtils; - networkList: AnvilNetworkConfigNetworkList; + networkList: ManifestNetworkList; setNetworkList: import('react').Dispatch< - import('react').SetStateAction + import('react').SetStateAction >; };