fix(frontend): updated to prune empty values from final result in network forms

pull/81/head
Samuel Rowe 3 years ago
parent bc89bd860a
commit c6f76fa7f3

@ -93,24 +93,21 @@ export const getInitialValues = (node?: INetworkNodeItem): IEditNetworkForm => {
networkName: name, networkName: name,
driver: ipam?.driver ?? "", driver: ipam?.driver ?? "",
configurations: configurations:
ipam?.config.map((item) => ({ ipam?.config?.map((item) => ({
subnet: item.subnet, subnet: item.subnet ?? "",
ipRange: item.ip_range, ipRange: item.ip_range ?? "",
gateway: item.gateway, gateway: item.gateway ?? "",
auxAddresses: Object.entries(item.aux_addresses).map( auxAddresses: Object.entries(item.aux_addresses ?? []).map(
([hostName, ipAddress]) => ({ ([hostName, ipAddress]) => ({
hostName, hostName,
ipAddress ipAddress
}) })
) )
})) ?? [], })) ?? [],
options: Object.keys(ipam?.options || {}).map((key) => { options: Object.keys(ipam?.options ?? {}).map((key) => {
if (!ipam) {
throw new Error("Control should not reach here.");
}
return { return {
key, key,
value: ipam.options[key].toString() value: ipam?.options?.[key].toString() ?? ""
}; };
}), }),
labels: Object.entries(labels as any).map(([key, value]: any) => ({ labels: Object.entries(labels as any).map(([key, value]: any) => ({
@ -129,34 +126,55 @@ export const getFinalValues = (
return { return {
key: previous?.key ?? "network", key: previous?.key ?? "network",
type: "NETWORK", type: "NETWORK",
position: {
left: 0,
top: 0
},
inputs: previous?.inputs ?? [], inputs: previous?.inputs ?? [],
outputs: previous?.outputs ?? [], outputs: previous?.outputs ?? [],
config: (previous as any)?.config ?? {},
canvasConfig: { canvasConfig: {
node_name: values.entryName node_name: values.entryName
}, },
networkConfig: { networkConfig: {
name: values.networkName, name: values.networkName,
ipam: { ipam: {
driver, driver: driver ? driver : undefined,
config: configurations.map((configuration) => ({ config: configurations.map((configuration) => ({
subnet: configuration.subnet, subnet: configuration.subnet ? configuration.subnet : undefined,
ip_range: configuration.ipRange, ip_range: configuration.ipRange ? configuration.ipRange : undefined,
gateway: configuration.gateway, gateway: configuration.gateway ? configuration.gateway : undefined,
aux_addresses: Object.fromEntries( aux_addresses: (() => {
configuration.auxAddresses.map((auxAddress) => [ if (configuration.auxAddresses.length === 0) {
auxAddress.hostName, return undefined;
auxAddress.ipAddress }
])
) /* We do not have to worry about empty `hostName` and `ipAddress`
* values because Yup would report such values as error.
*/
return Object.fromEntries(
configuration.auxAddresses.map((auxAddress) => [
auxAddress.hostName,
auxAddress.ipAddress
])
);
})()
})), })),
options: Object.fromEntries( options: (() => {
options.map((option) => [option.key, option.value]) if (options.length === 0) {
) return undefined;
}
/* We do not have to worry about empty `key` and `value`
* values because Yup would report such values as error.
*/
return Object.fromEntries(
options.map((option) => [option.key, option.value])
);
})()
}, },
labels: Object.fromEntries( labels: Object.fromEntries(
labels.map((label) => [label.key, label.value]) labels.map((label) => [label.key, label.value])
) )
} }
} as any; };
}; };

Loading…
Cancel
Save