| 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 { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox/index.js"; |
| 9 |
import { FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 10 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 11 |
|
| 12 |
import { NetworkModal, dialogSave } from './dialogs-common.jsx'; |
| 13 |
import { ModelContext } from './model-context.jsx'; |
| 14 |
import { useDialogs } from "dialogs.jsx"; |
| 15 |
|
| 16 |
const _ = cockpit.gettext; |
| 17 |
|
| 18 |
export const TeamPortDialog = ({ connection, dev, settings }) => { |
| 19 |
const Dialogs = useDialogs(); |
| 20 |
const idPrefix = "network-team-port-settings"; |
| 21 |
const model = useContext(ModelContext); |
| 22 |
|
| 23 |
const group_settings = connection.Groups[0].Settings; |
| 24 |
const group_config = group_settings.team.config; |
| 25 |
const teamMode = group_config.runner.name; |
| 26 |
let config = settings.team_port.config; |
| 27 |
|
| 28 |
if (!config) |
| 29 |
config = { }; |
| 30 |
|
| 31 |
const [priority, setPriority] = useState(teamMode == 'activebackup' ? config.prio : config.lacp_prio); |
| 32 |
const [sticky, setSticky] = useState(config.sticky); |
| 33 |
const [key, setKey] = useState(config.lacp_key); |
| 34 |
const [dialogError, setDialogError] = useState(undefined); |
| 35 |
|
| 36 |
const onSubmit = (ev) => { |
| 37 |
const createSettingsObj = () => ({ |
| 38 |
...settings, |
| 39 |
team_port: { |
| 40 |
config: { |
| 41 |
...config, |
| 42 |
...(teamMode == 'activebackup' && { prio: priority, sticky }), |
| 43 |
...(teamMode == 'lacp' && { lacp_prio: priority, lacp_key: key }), |
| 44 |
} |
| 45 |
} |
| 46 |
}); |
| 47 |
|
| 48 |
dialogSave({ |
| 49 |
model, |
| 50 |
dev, |
| 51 |
connection, |
| 52 |
settings: createSettingsObj(), |
| 53 |
setDialogError, |
| 54 |
onClose: Dialogs.close, |
| 55 |
}); |
| 56 |
|
| 57 |
// Prevent dialog from closing because of <form> onsubmit event |
| 58 |
if (ev) |
| 59 |
ev.preventDefault(); |
| 60 |
|
| 61 |
return false; |
| 62 |
}; |
| 63 |
|
| 64 |
return ( |
| 65 |
<NetworkModal dialogError={dialogError} |
| 66 |
idPrefix={idPrefix} |
| 67 |
onSubmit={onSubmit} |
| 68 |
title={_("Team port settings")} |
| 69 |
> |
| 70 |
<FormGroup fieldId={idPrefix + "-" + teamMode + "-prio-input"} label={_("Priority")}> |
| 71 |
<TextInput id={idPrefix + "-" + teamMode + "-prio-input"} value={priority} onChange={(_event, value) => setPriority(value)} /> |
| 72 |
</FormGroup> |
| 73 |
{teamMode == 'activebackup' |
| 74 |
? <FormGroup fieldId={idPrefix + "-activebackup-sticky-input"}> |
| 75 |
<Checkbox id={idPrefix + "-activebackup-sticky-input"} isChecked={sticky} onChange={(_, s) => setSticky(s)} label={_("Sticky")} /> |
| 76 |
</FormGroup> |
| 77 |
: null} |
| 78 |
{teamMode == 'lacp' |
| 79 |
? <FormGroup fieldId={idPrefix + "-" + teamMode + "-key-input"} label={_("LACP key")}> |
| 80 |
<TextInput id={idPrefix + "-" + teamMode + "-key-input"} value={key} onChange={(_event, value) => setKey(value)} /> |
| 81 |
</FormGroup> |
| 82 |
: null} |
| 83 |
</NetworkModal> |
| 84 |
); |
| 85 |
}; |