refactor(striker-ui): rename manifest network object and network event handlers

main
Tsu-ba-me 2 years ago
parent 20aba7c559
commit 172674db9a
  1. 4
      striker-ui/components/ManageManifest/AddManifestInputGroup.tsx
  2. 97
      striker-ui/components/ManageManifest/AnvilNetworkConfigInputGroup.tsx
  3. 28
      striker-ui/types/ManageManifest.d.ts

@ -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<AnvilNetworkConfigNetworkList>(previousNetworkList);
useState<ManifestNetworkList>(previousNetworkList);
return (
<FlexBox>

@ -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<AnvilNetworkConfigNetwork> = {}): {
network: AnvilNetworkConfigNetwork;
}: Partial<ManifestNetwork> = {}): {
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<AnvilNetworkTypeChangeHandler>(
(
{ networkId: targetId, networkType: previousType },
{ target: { value } },
) => {
const newType = String(value);
let isIdMatch = false;
let newTypeNumber = 0;
const handleNetworkTypeChange =
useCallback<AnvilNetworkTypeChangeEventHandler>(
(
{ networkId: targetId, networkType: previousType },
{ target: { value } },
) => {
const newType = String(value);
const newList = networkListEntries.reduce<AnvilNetworkConfigNetworkList>(
(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<ManifestNetworkList>(
(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<AnvilNetworkCloseHandler>(
const handleNetworkRemove = useCallback<AnvilNetworkCloseEventHandler>(
({ networkId: rmId, networkType: rmType }) => {
let isIdMatch = false;
let networkNumber = 0;
const newList = networkListEntries.reduce<AnvilNetworkConfigNetworkList>(
const newList = networkListEntries.reduce<ManifestNetworkList>(
(previous, [networkId, networkValue]) => {
if (networkId === rmId) {
isIdMatch = true;

@ -11,7 +11,7 @@ type AnvilIdInputGroupProps<M extends MapToInputTestID> =
formUtils: FormUtils<M>;
};
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<AnvilNetworkConfigNetwork, 'networkType'>,
type AnvilNetworkEventHandlerPreviousArgs = {
networkId: string;
} & Pick<ManifestNetwork, 'networkType'>;
type AnvilNetworkCloseEventHandler = (
args: AnvilNetworkEventHandlerPreviousArgs,
...handlerArgs: Parameters<IconButtonMouseEventHandler>
) => ReturnType<IconButtonMouseEventHandler>;
type AnvilNetworkTypeChangeHandler = (
args: { networkId: string } & Pick<AnvilNetworkConfigNetwork, 'networkType'>,
type AnvilNetworkTypeChangeEventHandler = (
args: AnvilNetworkEventHandlerPreviousArgs,
...handlerArgs: Parameters<SelectChangeEventHandler>
) => ReturnType<SelectChangeEventHandler>;
@ -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<M extends MapToInputTestID> =
AnvilNetworkConfigInputGroupOptionalProps & {
formUtils: FormUtils<M>;
networkList: AnvilNetworkConfigNetworkList;
networkList: ManifestNetworkList;
setNetworkList: import('react').Dispatch<
import('react').SetStateAction<AnvilNetworkConfigNetworkList>
import('react').SetStateAction<ManifestNetworkList>
>;
};

Loading…
Cancel
Save