2019-01-24 15:51:50 +00:00
|
|
|
import React from 'react';
|
2019-05-28 12:07:46 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-12-12 15:12:51 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-05-28 12:07:46 +00:00
|
|
|
import { Field, reduxForm, formValueSelector } from 'redux-form';
|
|
|
|
import { Trans, withNamespaces } from 'react-i18next';
|
2018-12-17 11:24:54 +00:00
|
|
|
import flow from 'lodash/flow';
|
|
|
|
|
2019-12-12 18:48:17 +00:00
|
|
|
import { renderInputField, required, ipv4, isPositive, toNumber } from '../../../helpers/form';
|
2018-12-12 15:12:51 +00:00
|
|
|
|
2019-05-28 12:07:46 +00:00
|
|
|
const renderInterfaces = (interfaces => (
|
|
|
|
Object.keys(interfaces).map((item) => {
|
|
|
|
const option = interfaces[item];
|
|
|
|
const { name } = option;
|
|
|
|
const onlyIPv6 = option.ip_addresses.every(ip => ip.includes(':'));
|
|
|
|
let interfaceIP = option.ip_addresses[0];
|
|
|
|
|
|
|
|
if (!onlyIPv6) {
|
|
|
|
option.ip_addresses.forEach((ip) => {
|
|
|
|
if (!ip.includes(':')) {
|
|
|
|
interfaceIP = ip;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<option value={name} key={name} disabled={onlyIPv6}>
|
|
|
|
{name} - {interfaceIP}
|
|
|
|
</option>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
));
|
|
|
|
|
|
|
|
const renderInterfaceValues = (interfaceValues => (
|
|
|
|
<ul className="list-unstyled mt-1 mb-0">
|
|
|
|
<li>
|
|
|
|
<span className="interface__title">MTU: </span>
|
|
|
|
{interfaceValues.mtu}
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<span className="interface__title"><Trans>dhcp_hardware_address</Trans>: </span>
|
|
|
|
{interfaceValues.hardware_address}
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<span className="interface__title"><Trans>dhcp_ip_addresses</Trans>: </span>
|
|
|
|
{
|
|
|
|
interfaceValues.ip_addresses
|
|
|
|
.map(ip => <span key={ip} className="interface__ip">{ip}</span>)
|
|
|
|
}
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
));
|
|
|
|
|
2019-10-15 14:14:24 +00:00
|
|
|
const clearFields = (change, resetDhcp, t) => {
|
|
|
|
const fields = {
|
|
|
|
interface_name: '',
|
|
|
|
gateway_ip: '',
|
|
|
|
subnet_mask: '',
|
|
|
|
range_start: '',
|
|
|
|
range_end: '',
|
|
|
|
lease_duration: 86400,
|
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-alert
|
|
|
|
if (window.confirm(t('dhcp_reset'))) {
|
|
|
|
Object.keys(fields).forEach(field => change(field, fields[field]));
|
|
|
|
resetDhcp();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-28 12:07:46 +00:00
|
|
|
let Form = (props) => {
|
2018-12-12 15:12:51 +00:00
|
|
|
const {
|
2018-12-24 08:48:23 +00:00
|
|
|
t,
|
|
|
|
handleSubmit,
|
|
|
|
submitting,
|
2019-01-23 13:35:50 +00:00
|
|
|
invalid,
|
2019-05-28 12:07:46 +00:00
|
|
|
enabled,
|
|
|
|
interfaces,
|
|
|
|
interfaceValue,
|
2019-01-23 14:22:04 +00:00
|
|
|
processingConfig,
|
2019-05-28 12:07:46 +00:00
|
|
|
processingInterfaces,
|
2019-10-15 14:14:24 +00:00
|
|
|
resetDhcp,
|
|
|
|
change,
|
2018-12-12 15:12:51 +00:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit}>
|
2019-05-28 12:07:46 +00:00
|
|
|
{!processingInterfaces && interfaces &&
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-sm-12 col-md-6">
|
|
|
|
<div className="form__group form__group--settings">
|
|
|
|
<label>{t('dhcp_interface_select')}</label>
|
|
|
|
<Field
|
|
|
|
name="interface_name"
|
|
|
|
component="select"
|
|
|
|
className="form-control custom-select"
|
|
|
|
validate={[required]}
|
|
|
|
>
|
|
|
|
<option value="" disabled={enabled}>
|
|
|
|
{t('dhcp_interface_select')}
|
|
|
|
</option>
|
|
|
|
{renderInterfaces(interfaces)}
|
|
|
|
</Field>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{interfaceValue &&
|
|
|
|
<div className="col-sm-12 col-md-6">
|
|
|
|
{interfaces[interfaceValue] &&
|
|
|
|
renderInterfaceValues(interfaces[interfaceValue])}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
<hr/>
|
2018-12-12 15:12:51 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-lg-6">
|
2019-01-24 15:51:50 +00:00
|
|
|
<div className="form__group form__group--settings">
|
2018-12-17 11:24:54 +00:00
|
|
|
<label>{t('dhcp_form_gateway_input')}</label>
|
2018-12-12 15:12:51 +00:00
|
|
|
<Field
|
2019-12-12 18:48:17 +00:00
|
|
|
id="gateway_ip"
|
2018-12-12 15:12:51 +00:00
|
|
|
name="gateway_ip"
|
2019-12-12 18:48:17 +00:00
|
|
|
component={renderInputField}
|
2018-12-12 15:12:51 +00:00
|
|
|
type="text"
|
|
|
|
className="form-control"
|
2018-12-17 11:24:54 +00:00
|
|
|
placeholder={t('dhcp_form_gateway_input')}
|
2018-12-12 15:12:51 +00:00
|
|
|
validate={[ipv4, required]}
|
|
|
|
/>
|
|
|
|
</div>
|
2019-01-24 15:51:50 +00:00
|
|
|
<div className="form__group form__group--settings">
|
2018-12-17 11:24:54 +00:00
|
|
|
<label>{t('dhcp_form_subnet_input')}</label>
|
2018-12-12 15:12:51 +00:00
|
|
|
<Field
|
2019-12-12 18:48:17 +00:00
|
|
|
id="subnet_mask"
|
2018-12-12 15:12:51 +00:00
|
|
|
name="subnet_mask"
|
2019-12-12 18:48:17 +00:00
|
|
|
component={renderInputField}
|
2018-12-12 15:12:51 +00:00
|
|
|
type="text"
|
|
|
|
className="form-control"
|
2018-12-17 11:24:54 +00:00
|
|
|
placeholder={t('dhcp_form_subnet_input')}
|
2018-12-12 15:12:51 +00:00
|
|
|
validate={[ipv4, required]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="col-lg-6">
|
2019-01-24 15:51:50 +00:00
|
|
|
<div className="form__group form__group--settings">
|
2018-12-12 15:12:51 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-12">
|
2018-12-17 11:24:54 +00:00
|
|
|
<label>{t('dhcp_form_range_title')}</label>
|
2018-12-12 15:12:51 +00:00
|
|
|
</div>
|
|
|
|
<div className="col">
|
|
|
|
<Field
|
2019-12-12 18:48:17 +00:00
|
|
|
id="range_start"
|
2018-12-12 15:12:51 +00:00
|
|
|
name="range_start"
|
2019-12-12 18:48:17 +00:00
|
|
|
component={renderInputField}
|
2018-12-12 15:12:51 +00:00
|
|
|
type="text"
|
|
|
|
className="form-control"
|
2018-12-17 11:24:54 +00:00
|
|
|
placeholder={t('dhcp_form_range_start')}
|
2018-12-12 15:12:51 +00:00
|
|
|
validate={[ipv4, required]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col">
|
|
|
|
<Field
|
2019-12-12 18:48:17 +00:00
|
|
|
id="range_end"
|
2018-12-12 15:12:51 +00:00
|
|
|
name="range_end"
|
2019-12-12 18:48:17 +00:00
|
|
|
component={renderInputField}
|
2018-12-12 15:12:51 +00:00
|
|
|
type="text"
|
|
|
|
className="form-control"
|
2018-12-17 11:24:54 +00:00
|
|
|
placeholder={t('dhcp_form_range_end')}
|
2018-12-12 15:12:51 +00:00
|
|
|
validate={[ipv4, required]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-24 15:51:50 +00:00
|
|
|
<div className="form__group form__group--settings">
|
2018-12-17 11:24:54 +00:00
|
|
|
<label>{t('dhcp_form_lease_title')}</label>
|
2018-12-12 15:12:51 +00:00
|
|
|
<Field
|
|
|
|
name="lease_duration"
|
2019-12-12 18:48:17 +00:00
|
|
|
component={renderInputField}
|
2018-12-12 15:12:51 +00:00
|
|
|
type="number"
|
|
|
|
className="form-control"
|
2018-12-17 11:24:54 +00:00
|
|
|
placeholder={t('dhcp_form_lease_input')}
|
2018-12-13 12:26:47 +00:00
|
|
|
validate={[required, isPositive]}
|
|
|
|
normalize={toNumber}
|
2018-12-12 15:12:51 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2019-10-15 14:14:24 +00:00
|
|
|
<div className="btn-list">
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-success btn-standard"
|
|
|
|
disabled={submitting || invalid || processingConfig}
|
|
|
|
>
|
|
|
|
{t('save_config')}
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="btn btn-secondary btn-standart"
|
|
|
|
disabled={submitting || processingConfig}
|
|
|
|
onClick={() => clearFields(change, resetDhcp, t)}
|
|
|
|
>
|
|
|
|
<Trans>reset_settings</Trans>
|
|
|
|
</button>
|
|
|
|
</div>
|
2018-12-12 15:12:51 +00:00
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Form.propTypes = {
|
2019-10-15 14:14:24 +00:00
|
|
|
handleSubmit: PropTypes.func.isRequired,
|
|
|
|
submitting: PropTypes.bool.isRequired,
|
|
|
|
invalid: PropTypes.bool.isRequired,
|
|
|
|
interfaces: PropTypes.object.isRequired,
|
2019-05-28 12:07:46 +00:00
|
|
|
interfaceValue: PropTypes.string,
|
2019-10-15 14:14:24 +00:00
|
|
|
initialValues: PropTypes.object.isRequired,
|
|
|
|
processingConfig: PropTypes.bool.isRequired,
|
|
|
|
processingInterfaces: PropTypes.bool.isRequired,
|
|
|
|
enabled: PropTypes.bool.isRequired,
|
|
|
|
t: PropTypes.func.isRequired,
|
|
|
|
resetDhcp: PropTypes.func.isRequired,
|
|
|
|
change: PropTypes.func.isRequired,
|
2018-12-12 15:12:51 +00:00
|
|
|
};
|
|
|
|
|
2019-05-28 12:07:46 +00:00
|
|
|
const selector = formValueSelector('dhcpForm');
|
|
|
|
|
|
|
|
Form = connect((state) => {
|
|
|
|
const interfaceValue = selector(state, 'interface_name');
|
|
|
|
return {
|
|
|
|
interfaceValue,
|
|
|
|
};
|
|
|
|
})(Form);
|
|
|
|
|
2018-12-17 11:24:54 +00:00
|
|
|
export default flow([
|
|
|
|
withNamespaces(),
|
|
|
|
reduxForm({ form: 'dhcpForm' }),
|
|
|
|
])(Form);
|