LCOV - code coverage report
Current view: top level - pkg/networkmanager - firewall-switch.jsx Coverage Total Hit
Test: cockpit Lines: 100.0 % 51 51
Test Date: 2026-07-13 10:00:01

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

Generated by: LCOV version 2.0-1