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 { Radio } from "@patternfly/react-core/dist/esm/components/Radio/index.js";
9 : import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js";
10 :
11 : import { NetworkModal, dialogSave } from './dialogs-common.jsx';
12 : import { ModelContext } from './model-context.jsx';
13 : import { useDialogs } from "dialogs.jsx";
14 :
15 1 : const _ = cockpit.gettext;
16 :
17 0 : export const MtuDialog = ({ connection, dev, settings }) => {
18 0 : const Dialogs = useDialogs();
19 0 : const idPrefix = "network-mtu-settings";
20 0 : const model = useContext(ModelContext);
21 :
22 0 : const [dialogError, setDialogError] = useState(undefined);
23 0 : const [mode, setMode] = useState(!settings.ethernet.mtu ? "auto" : "custom");
24 0 : const [mtu, setMtu] = useState(settings.ethernet.mtu ? settings.ethernet.mtu : '');
25 :
26 0 : const onSubmit = (ev) => {
27 0 : const mtuNew = mode == 'auto' ? 0 : parseInt(mtu, 10);
28 0 : if (isNaN(mtuNew) || mtuNew < 0) {
29 0 : setDialogError(_("MTU must be a positive number"));
30 0 : return;
31 0 : }
32 0 : const createSettingsObj = () => ({
33 0 : ...settings,
34 0 : ethernet: {
35 0 : ...settings.ethernet,
36 0 : mtu: mtuNew,
37 0 : }
38 0 : });
39 :
40 0 : dialogSave({
41 0 : model,
42 0 : dev,
43 0 : connection,
44 0 : settings: createSettingsObj(),
45 0 : setDialogError,
46 0 : onClose: Dialogs.close,
47 0 : });
48 :
49 : // Prevent dialog from closing because of <form> onsubmit event
50 0 : if (ev)
51 0 : ev.preventDefault();
52 :
53 0 : return false;
54 0 : };
55 :
56 0 : return (
57 0 : <NetworkModal dialogError={dialogError}
58 0 : idPrefix={idPrefix}
59 0 : onSubmit={onSubmit}
60 0 : title={_("Ethernet MTU")}
61 : >
62 0 : <>
63 0 : <Radio id={idPrefix + "-auto"}
64 0 : isChecked={mode == "auto"}
65 0 : label={_("Automatic")}
66 0 : name="mtu-mode"
67 0 : onChange={() => setMode("auto")}
68 0 : value="auto" />
69 0 : <Radio id={idPrefix + "-custom"}
70 0 : isChecked={mode == "custom"}
71 0 : className="ct-align-center"
72 0 : label={
73 0 : <>
74 0 : <span>{_("Set to")}</span>
75 0 : <TextInput id={idPrefix + "-input"} value={mtu} onChange={(_event, value) => setMtu(value)} className="mtu-label-input" />
76 0 : </>
77 : }
78 0 : name="mtu-mode"
79 0 : onChange={() => setMode("custom")}
80 0 : value="custom" />
81 0 : </>
82 0 : </NetworkModal>
83 : );
84 0 : };
|