Debian Codegraph @

pkg/networkmanager/vlan.jsx

raw
1 /*
2 * Copyright (C) 2021 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import React, { useState, useContext } from 'react';
7 import cockpit from 'cockpit';
8 import { FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js";
9 import { FormSelect, FormSelectOption } from "@patternfly/react-core/dist/esm/components/FormSelect/index.js";
10 import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js";
11
12 import { Name, NetworkModal, dialogSave } from './dialogs-common.jsx';
13 import { ModelContext } from './model-context.jsx';
14 import { useDialogs } from "dialogs.jsx";
15
16 import { v4 as uuidv4 } from 'uuid';
17 import {
18 is_interface_connection,
19 is_interesting_interface,
20 } from './interfaces.js';
21
22 const _ = cockpit.gettext;
23
24 export const VlanDialog = ({ connection, dev, settings }) => {
25 const Dialogs = useDialogs();
26 const idPrefix = "network-vlan-settings";
27 const model = useContext(ModelContext);
28 const parentChoices = [];
29 model.list_interfaces().forEach(iface => {
30 if (!is_interface_connection(iface, connection) &&
31 is_interesting_interface(iface))
32 parentChoices.push(iface.Name);
33 });
34
35 const [dialogError, setDialogError] = useState(undefined);
36 const [parent, setParent] = useState(settings.vlan.parent || parentChoices[0]);
37 const [vlanId, setVlanId] = useState(settings.vlan.id || 1);
38 const [iface, setIface] = useState(settings.vlan.interface_name || (parent + "." + vlanId));
39
40 const onSubmit = (ev) => {
41 const createSettingsObj = () => ({
42 ...settings,
43 connection: {
44 ...settings.connection,
45 id: iface,
46 interface_name: iface,
47 },
48 vlan: {
49 ...settings.vlan,
50 parent,
51 id: parseInt(vlanId, 10),
52 interface_name: iface,
53 }
54 });
55
56 dialogSave({
57 model,
58 dev,
59 connection,
60 settings: createSettingsObj(),
61 setDialogError,
62 onClose: Dialogs.close,
63 });
64
65 // Prevent dialog from closing because of <form> onsubmit event
66 if (ev)
67 ev.preventDefault();
68
69 return false;
70 };
71
72 return (
73 <NetworkModal dialogError={dialogError}
74 idPrefix={idPrefix}
75 onSubmit={onSubmit}
76 title={!connection ? _("Add VLAN") : _("Edit VLAN settings")}
77 isCreateDialog={!connection}
78 >
79 <>
80 <FormGroup fieldId={idPrefix + "-parent-select"} label={_("Parent")}>
81 <FormSelect id={idPrefix + "-parent-select"} onChange={(_, value) => {
82 setParent(value);
83 if (iface == (parent + "." + vlanId))
84 setIface(value + "." + vlanId);
85 }}
86 value={parent}>
87 {parentChoices.map(choice => <FormSelectOption value={choice} label={choice} key={choice} />)}
88 </FormSelect>
89 </FormGroup>
90 <FormGroup fieldId={idPrefix + "-vlan-id-input"} label={_("VLAN ID")}>
91 <TextInput id={idPrefix + "-vlan-id-input"} value={vlanId} onChange={(_event, value) => {
92 setVlanId(value);
93 if (iface == (parent + "." + vlanId))
94 setIface(parent + "." + value);
95 }} />
96 </FormGroup>
97 <Name idPrefix={idPrefix} iface={iface} setIface={setIface} />
98 </>
99 </NetworkModal>
100 );
101 };
102
103 export const getGhostSettings = () => {
104 return (
105 {
106 connection: {
107 id: "",
108 autoconnect: true,
109 type: "vlan",
110 uuid: uuidv4(),
111 interface_name: ""
112 },
113 vlan: {
114 interface_name: "",
115 parent: ""
116 }
117 }
118 );
119 };