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