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