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