Skip to content

Commit 26d2c33

Browse files
committed
nics: Refactor NetworkTypeAndSource
To have only one dialog value instead of three (plus assorted props). The goal is to put the initialization knowledge in the component itself, and to not have any extra arguments for the validation function. This gives this component the "standard" API.
1 parent 39a5147 commit 26d2c33

File tree

5 files changed

+210
-199
lines changed

5 files changed

+210
-199
lines changed

src/components/vm/nics/nicAdd.tsx

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import { Radio } from "@patternfly/react-core/dist/esm/components/Radio";
3232
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput";
3333

3434
import {
35-
NetworkTypeAndSourceRow, validate_NetworkTypeAndSourceRow,
35+
NetworkTypeAndSourceValue, NetworkTypeAndSourceRow,
36+
init_NetworkTypeAndSourceRow, validate_NetworkTypeAndSourceRow,
3637
NetworkModelRow,
3738
PortForwardsValue, NetworkPortForwardsRow, validate_PortForwards,
3839
dialogPortForwardsToInterface,
@@ -159,9 +160,7 @@ const PermanentChange = ({
159160

160161
interface AddNICValues {
161162
model: string,
162-
type: string;
163-
source: string;
164-
source_mode: string;
163+
type_and_source: NetworkTypeAndSourceValue,
165164
mac: NetworkMacValue;
166165
portForwards: PortForwardsValue;
167166
permanent: boolean;
@@ -183,9 +182,7 @@ export const AddNIC = ({
183182
function init(): AddNICValues {
184183
return {
185184
model: "virtio",
186-
type: vm.connectionName == "session" ? "user" : "network",
187-
source: availableSources.network.length > 0 ? availableSources.network[0] : "",
188-
source_mode: "bridge",
185+
type_and_source: init_NetworkTypeAndSourceRow(vm, null, availableSources),
189186
mac: init_NetworkMacRow(),
190187
portForwards: [],
191188
permanent: false,
@@ -194,8 +191,8 @@ export const AddNIC = ({
194191

195192
function validate() {
196193
validate_NetworkMacRow(dlg.value("mac"));
197-
validate_NetworkTypeAndSourceRow(dlg.value("source"), vm, availableSources);
198-
if (dlg.value("type").get() == "user")
194+
validate_NetworkTypeAndSourceRow(dlg.value("type_and_source"));
195+
if (dlg.values.type_and_source.type == "user")
199196
validate_PortForwards(dlg.value("portForwards"));
200197
}
201198

@@ -207,17 +204,18 @@ export const AddNIC = ({
207204
throw new DialogError(_("MAC address already in use"), _("Please choose a different MAC address"));
208205

209206
try {
207+
const tas = values.type_and_source;
210208
let source;
211-
if (values.type == "direct") {
209+
if (tas.type == "direct") {
212210
source = {
213-
"": values.source,
214-
mode: values.source_mode,
211+
"": tas.source,
212+
mode: tas.mode,
215213
};
216214
} else {
217-
source = values.source;
215+
source = tas.source;
218216
}
219217
let backend = null;
220-
if (values.type == "user" && vm.capabilities.interfaceBackends.includes("passt"))
218+
if (tas.type == "user" && vm.capabilities.interfaceBackends.includes("passt"))
221219
backend = "passt";
222220
await virtXmlHotAdd(
223221
vm,
@@ -229,11 +227,11 @@ export const AddNIC = ({
229227
: getRandomMac(await appState.getVms())
230228
),
231229
model: values.model,
232-
type: values.type,
230+
type: tas.type,
233231
backend: { type: backend },
234232
source,
235233
portForward: (
236-
values.type == "user"
234+
tas.type == "user"
237235
? dialogPortForwardsToInterface(values.portForwards)
238236
: null
239237
),
@@ -251,13 +249,7 @@ export const AddNIC = ({
251249
const defaultBody = (
252250
<>
253251
<Form onSubmit={e => e.preventDefault()} isHorizontal>
254-
<NetworkTypeAndSourceRow
255-
vm={vm}
256-
type_value={dlg.value("type")}
257-
source_value={dlg.value("source")}
258-
source_mode_value={dlg.value("source_mode")}
259-
availableSources={availableSources}
260-
/>
252+
<NetworkTypeAndSourceRow value={dlg.value("type_and_source")} />
261253

262254
<NetworkModelRow
263255
value={dlg.value("model")}
@@ -271,7 +263,7 @@ export const AddNIC = ({
271263
<PermanentChange value={dlg.value("permanent")} />
272264
}
273265
</Form>
274-
{ dlg.values.type == "user" &&
266+
{ dlg.values.type_and_source.type == "user" &&
275267
vm.capabilities.interfaceBackends.includes("passt") &&
276268
<Form>
277269
<br />

0 commit comments

Comments
 (0)