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 41 : constructor() {
14 41 : super();
15 41 : this.state = {
16 41 : pendingTarget: null /* `null` for not pending */
17 41 : };
18 41 : this.onSwitchChanged = this.onSwitchChanged.bind(this);
19 41 : }
20 :
21 41 : static getDerivedStateFromProps(props, state) {
22 8 : if (props.firewall.enabled == state.pendingTarget) {
23 8 : return {
24 8 : pendingTarget: null
25 8 : };
26 8 : }
27 41 : return null;
28 41 : }
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 41 : render() {
40 8 : const enabled = this.state.pendingTarget !== null ? this.state.pendingTarget : this.props.firewall.enabled;
41 41 : let firewallOnOff;
42 :
43 10 : if (this.props.firewall.readonly) {
44 10 : firewallOnOff = <Tooltip id="tip-auth"
45 10 : content={ _("You are not authorized to modify the firewall.") }>
46 10 : <Switch isChecked={enabled}
47 10 : id='networking-firewall-switch'
48 10 : className='networking-firewall-switch'
49 10 : onChange={this.onSwitchChanged}
50 8 : label={enabled ? _("Enabled") : _("Disabled")}
51 8 : aria-label={enabled ? _("Not authorized to disable the firewall") : _("Not authorized to enable the firewall")}
52 10 : isDisabled />
53 10 : </Tooltip>;
54 9 : } else {
55 40 : firewallOnOff = <Switch isChecked={enabled}
56 40 : id='networking-firewall-switch'
57 40 : className='networking-firewall-switch'
58 40 : isDisabled={!!this.state.pendingTarget}
59 40 : onChange={this.onSwitchChanged}
60 8 : label={enabled ? _("Enabled") : _("Disabled")}
61 8 : aria-label={enabled ? _("Disable the firewall") : _("Enable the firewall")} />;
62 40 : }
63 41 : return firewallOnOff;
64 41 : }
65 42 : }
|