| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
import cockpit from "cockpit"; |
| 6 |
import React from "react"; |
| 7 |
import { Switch } from "@patternfly/react-core/dist/esm/components/Switch/index.js"; |
| 8 |
import { Tooltip } from "@patternfly/react-core/dist/esm/components/Tooltip/index.js"; |
| 9 |
|
| 10 |
const _ = cockpit.gettext; |
| 11 |
|
| 12 |
export class FirewallSwitch extends React.Component { |
| 13 |
constructor() { |
| 14 |
super(); |
| 15 |
this.state = { |
| 16 |
pendingTarget: null /* `null` for not pending */ |
| 17 |
}; |
| 18 |
this.onSwitchChanged = this.onSwitchChanged.bind(this); |
| 19 |
} |
| 20 |
|
| 21 |
static getDerivedStateFromProps(props, state) { |
| 22 |
if (props.firewall.enabled == state.pendingTarget) { |
| 23 |
return { |
| 24 |
pendingTarget: null |
| 25 |
}; |
| 26 |
} |
| 27 |
return null; |
| 28 |
} |
| 29 |
|
| 30 |
onSwitchChanged(_event, value) { |
| 31 |
this.setState({ pendingTarget: value }); |
| 32 |
|
| 33 |
if (value) |
| 34 |
this.props.firewall.enable(); |
| 35 |
else |
| 36 |
this.props.firewall.disable(); |
| 37 |
} |
| 38 |
|
| 39 |
render() { |
| 40 |
const enabled = this.state.pendingTarget !== null ? this.state.pendingTarget : this.props.firewall.enabled; |
| 41 |
let firewallOnOff; |
| 42 |
|
| 43 |
if (this.props.firewall.readonly) { |
| 44 |
firewallOnOff = <Tooltip id="tip-auth" |
| 45 |
content={ _("You are not authorized to modify the firewall.") }> |
| 46 |
<Switch isChecked={enabled} |
| 47 |
id='networking-firewall-switch' |
| 48 |
className='networking-firewall-switch' |
| 49 |
onChange={this.onSwitchChanged} |
| 50 |
label={enabled ? _("Enabled") : _("Disabled")} |
| 51 |
aria-label={enabled ? _("Not authorized to disable the firewall") : _("Not authorized to enable the firewall")} |
| 52 |
isDisabled /> |
| 53 |
</Tooltip>; |
| 54 |
} else { |
| 55 |
firewallOnOff = <Switch isChecked={enabled} |
| 56 |
id='networking-firewall-switch' |
| 57 |
className='networking-firewall-switch' |
| 58 |
isDisabled={!!this.state.pendingTarget} |
| 59 |
onChange={this.onSwitchChanged} |
| 60 |
label={enabled ? _("Enabled") : _("Disabled")} |
| 61 |
aria-label={enabled ? _("Disable the firewall") : _("Enable the firewall")} />; |
| 62 |
} |
| 63 |
return firewallOnOff; |
| 64 |
} |
| 65 |
} |