import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Trans, withTranslation } from 'react-i18next';
import { DHCP_STATUS_RESPONSE } from '../../../helpers/constants';
import Form from './Form';
import Leases from './Leases';
import StaticLeases from './StaticLeases/index';
import Card from '../../ui/Card';
import Accordion from '../../ui/Accordion';
import PageTitle from '../../ui/PageTitle';
import Loading from '../../ui/Loading';
class Dhcp extends Component {
componentDidMount() {
this.props.getDhcpStatus();
this.props.getDhcpInterfaces();
}
handleFormSubmit = (values) => {
if (values.interface_name) {
this.props.setDhcpConfig(values);
}
};
handleToggle = (config) => {
this.props.toggleDhcp(config);
};
getToggleDhcpButton = () => {
const {
config, check, processingDhcp, processingConfig,
} = this.props.dhcp;
const otherDhcpFound = check?.otherServer
&& check.otherServer.found === DHCP_STATUS_RESPONSE.YES;
const filledConfig = Object.keys(config).every((key) => {
if (key === 'enabled' || key === 'icmp_timeout_msec') {
return true;
}
return config[key];
});
if (config.enabled) {
return (
);
}
return (
);
};
getActiveDhcpMessage = (t, check) => {
const { found } = check.otherServer;
if (found === DHCP_STATUS_RESPONSE.ERROR) {
return (
dhcp_error
{check.otherServer.error}
);
}
return (
{found === DHCP_STATUS_RESPONSE.YES ? (
dhcp_found
) : (
dhcp_not_found
)}
);
};
getDhcpWarning = (check) => {
if (check.otherServer.found === DHCP_STATUS_RESPONSE.NO) {
return '';
}
return (
dhcp_warning
);
};
getStaticIpWarning = (t, check, interfaceName) => {
if (check.staticIP.static === DHCP_STATUS_RESPONSE.ERROR) {
return (
);
} if (
check.staticIP.static === DHCP_STATUS_RESPONSE.NO
&& check.staticIP.ip
&& interfaceName
) {
return (
example]}
values={{
interfaceName,
ipAddress: check.staticIP.ip,
}}
>
dhcp_dynamic_ip_found
);
}
return '';
};
render() {
const {
t,
dhcp,
resetDhcp,
findActiveDhcp,
addStaticLease,
removeStaticLease,
toggleLeaseModal,
} = this.props;
const statusButtonClass = classnames({
'btn btn-primary btn-standard': true,
'btn btn-primary btn-standard btn-loading': dhcp.processingStatus,
});
const { enabled, interface_name, ...values } = dhcp.config;
return (
{(dhcp.processing || dhcp.processingInterfaces) && }
{!dhcp.processing && !dhcp.processingInterfaces && (
{this.getToggleDhcpButton()}
{!enabled && dhcp.check && (
{this.getStaticIpWarning(t, dhcp.check, interface_name)}
{this.getActiveDhcpMessage(t, dhcp.check)}
{this.getDhcpWarning(dhcp.check)}
)}
{dhcp.config.enabled && (
)}
)}
);
}
}
Dhcp.propTypes = {
dhcp: PropTypes.object.isRequired,
toggleDhcp: PropTypes.func.isRequired,
getDhcpStatus: PropTypes.func.isRequired,
setDhcpConfig: PropTypes.func.isRequired,
findActiveDhcp: PropTypes.func.isRequired,
addStaticLease: PropTypes.func.isRequired,
removeStaticLease: PropTypes.func.isRequired,
toggleLeaseModal: PropTypes.func.isRequired,
getDhcpInterfaces: PropTypes.func.isRequired,
t: PropTypes.func.isRequired,
resetDhcp: PropTypes.func.isRequired,
};
export default withTranslation()(Dhcp);