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