badguardhome/client/src/components/Settings/Dhcp/index.js

284 lines
11 KiB
JavaScript
Raw Normal View History

import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Trans, withNamespaces } from 'react-i18next';
2019-04-05 11:32:56 +00:00
import { DHCP_STATUS_RESPONSE } from '../../../helpers/constants';
import Form from './Form';
import Leases from './Leases';
2019-05-28 12:07:46 +00:00
import StaticLeases from './StaticLeases/index';
import Card from '../../ui/Card';
2019-03-28 13:30:22 +00:00
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) => {
2019-05-28 12:07:46 +00:00
if (values.interface_name) {
this.props.setDhcpConfig(values);
}
};
handleToggle = (config) => {
this.props.toggleDhcp(config);
};
getToggleDhcpButton = () => {
2019-01-23 14:22:04 +00:00
const {
2019-04-04 13:34:46 +00:00
config, check, processingDhcp, processingConfig,
2019-01-23 14:22:04 +00:00
} = this.props.dhcp;
2019-04-04 13:34:46 +00:00
const otherDhcpFound =
2019-04-05 11:32:56 +00:00
check && check.otherServer && check.otherServer.found === DHCP_STATUS_RESPONSE.YES;
const filledConfig = Object.keys(config).every((key) => {
2019-03-28 13:32:22 +00:00
if (key === 'enabled' || key === 'icmp_timeout_msec') {
return true;
}
return config[key];
});
if (config.enabled) {
return (
<button
type="button"
2019-01-18 17:17:48 +00:00
className="btn btn-standard mr-2 btn-gray"
onClick={() => this.props.toggleDhcp(config)}
2019-01-23 14:22:04 +00:00
disabled={processingDhcp || processingConfig}
>
<Trans>dhcp_disable</Trans>
</button>
);
}
return (
2018-12-28 16:48:02 +00:00
<button
type="button"
2019-01-18 17:17:48 +00:00
className="btn btn-standard mr-2 btn-success"
onClick={() => this.handleToggle(config)}
2019-01-23 14:22:04 +00:00
disabled={
!filledConfig || !check || otherDhcpFound || processingDhcp || processingConfig
2019-01-23 14:22:04 +00:00
}
2018-12-28 16:48:02 +00:00
>
<Trans>dhcp_enable</Trans>
</button>
);
};
2019-04-04 13:34:46 +00:00
getActiveDhcpMessage = (t, check) => {
const { found } = check.otherServer;
2019-04-05 11:32:56 +00:00
if (found === DHCP_STATUS_RESPONSE.ERROR) {
return (
2019-04-04 13:34:46 +00:00
<div className="text-danger mb-2">
<Trans>dhcp_error</Trans>
<div className="mt-2 mb-2">
<Accordion label={t('error_details')}>
<span>{check.otherServer.error}</span>
</Accordion>
</div>
2019-01-28 11:19:31 +00:00
</div>
);
}
2019-04-04 13:34:46 +00:00
return (
<div className="mb-2">
2019-04-05 11:32:56 +00:00
{found === DHCP_STATUS_RESPONSE.YES ? (
2019-04-04 13:34:46 +00:00
<div className="text-danger">
<Trans>dhcp_found</Trans>
</div>
) : (
<div className="text-secondary">
<Trans>dhcp_not_found</Trans>
</div>
)}
</div>
);
};
2019-04-04 13:34:46 +00:00
getDhcpWarning = (check) => {
2019-04-05 11:32:56 +00:00
if (check.otherServer.found === DHCP_STATUS_RESPONSE.NO) {
2019-03-28 13:30:22 +00:00
return '';
}
return (
<div className="text-danger">
<Trans>dhcp_warning</Trans>
</div>
);
};
2019-03-28 13:30:22 +00:00
2019-04-04 13:34:46 +00:00
getStaticIpWarning = (t, check, interfaceName) => {
2019-04-05 11:32:56 +00:00
if (check.staticIP.static === DHCP_STATUS_RESPONSE.ERROR) {
2019-04-04 13:34:46 +00:00
return (
<Fragment>
<div className="text-danger mb-2">
<Trans>dhcp_static_ip_error</Trans>
<div className="mt-2 mb-2">
<Accordion label={t('error_details')}>
<span>{check.staticIP.error}</span>
</Accordion>
</div>
</div>
<hr className="mt-4 mb-4" />
2019-04-04 13:34:46 +00:00
</Fragment>
);
} else if (
check.staticIP.static === DHCP_STATUS_RESPONSE.NO &&
check.staticIP.ip &&
interfaceName
2019-04-04 13:34:46 +00:00
) {
return (
<Fragment>
<div className="text-secondary mb-2">
<Trans
components={[<strong key="0">example</strong>]}
2019-04-04 13:34:46 +00:00
values={{
interfaceName,
ipAddress: check.staticIP.ip,
}}
>
dhcp_dynamic_ip_found
</Trans>
</div>
<hr className="mt-4 mb-4" />
2019-04-04 13:34:46 +00:00
</Fragment>
);
}
return '';
};
2019-04-04 13:34:46 +00:00
render() {
2019-10-15 14:14:24 +00:00
const {
t,
dhcp,
resetDhcp,
findActiveDhcp,
addStaticLease,
removeStaticLease,
toggleLeaseModal,
} = this.props;
const statusButtonClass = classnames({
2019-01-18 17:17:48 +00:00
'btn btn-primary btn-standard': true,
'btn btn-primary btn-standard btn-loading': dhcp.processingStatus,
});
const { enabled, interface_name, ...values } = dhcp.config;
return (
<Fragment>
<PageTitle title={t('dhcp_settings')} />
{(dhcp.processing || dhcp.processingInterfaces) && <Loading />}
{!dhcp.processing && !dhcp.processingInterfaces && (
<Fragment>
<Card
title={t('dhcp_title')}
subtitle={t('dhcp_description')}
bodyType="card-body box-body--settings"
>
<div className="dhcp">
<Fragment>
<Form
onSubmit={this.handleFormSubmit}
initialValues={{
interface_name,
...values,
}}
interfaces={dhcp.interfaces}
processingConfig={dhcp.processingConfig}
processingInterfaces={dhcp.processingInterfaces}
enabled={enabled}
2019-10-15 14:14:24 +00:00
resetDhcp={resetDhcp}
2019-05-28 12:07:46 +00:00
/>
<hr />
<div className="card-actions mb-3">
{this.getToggleDhcpButton()}
<button
type="button"
className={statusButtonClass}
2019-10-15 14:14:24 +00:00
onClick={() => findActiveDhcp(interface_name)}
disabled={
enabled || !interface_name || dhcp.processingConfig
}
>
<Trans>check_dhcp_servers</Trans>
</button>
</div>
{!enabled && dhcp.check && (
<Fragment>
{this.getStaticIpWarning(t, dhcp.check, interface_name)}
{this.getActiveDhcpMessage(t, dhcp.check)}
{this.getDhcpWarning(dhcp.check)}
</Fragment>
)}
</Fragment>
</div>
</Card>
{dhcp.config.enabled && (
<Fragment>
<Card
title={t('dhcp_leases')}
bodyType="card-body box-body--settings"
>
<div className="row">
<div className="col">
<Leases leases={dhcp.leases} />
</div>
</div>
</Card>
<Card
title={t('dhcp_static_leases')}
bodyType="card-body box-body--settings"
>
<div className="row">
<div className="col-12">
<StaticLeases
staticLeases={dhcp.staticLeases}
isModalOpen={dhcp.isModalOpen}
2019-10-15 14:14:24 +00:00
addStaticLease={addStaticLease}
removeStaticLease={removeStaticLease}
toggleLeaseModal={toggleLeaseModal}
processingAdding={dhcp.processingAdding}
processingDeleting={dhcp.processingDeleting}
/>
</div>
<div className="col-12">
<button
type="button"
className="btn btn-success btn-standard mt-3"
2019-10-15 14:14:24 +00:00
onClick={() => toggleLeaseModal()}
>
<Trans>dhcp_add_static_lease</Trans>
</button>
</div>
</div>
</Card>
</Fragment>
)}
</Fragment>
)}
</Fragment>
);
}
}
Dhcp.propTypes = {
2019-10-15 14:14:24 +00:00
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 withNamespaces()(Dhcp);