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