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