2018-12-12 15:12:51 +00:00
|
|
|
import React, { Fragment } from 'react';
|
2018-12-24 08:48:23 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-12-12 15:12:51 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-12-24 08:48:23 +00:00
|
|
|
import { Field, reduxForm, formValueSelector } from 'redux-form';
|
2018-12-17 11:24:54 +00:00
|
|
|
import { withNamespaces, Trans } from 'react-i18next';
|
|
|
|
import flow from 'lodash/flow';
|
|
|
|
|
2018-12-12 15:12:51 +00:00
|
|
|
import { R_IPV4 } from '../../../helpers/constants';
|
|
|
|
|
|
|
|
const required = (value) => {
|
2018-12-13 12:26:47 +00:00
|
|
|
if (value || value === 0) {
|
2018-12-12 15:12:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-17 11:24:54 +00:00
|
|
|
return <Trans>form_error_required</Trans>;
|
2018-12-12 15:12:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const ipv4 = (value) => {
|
|
|
|
if (value && !new RegExp(R_IPV4).test(value)) {
|
2018-12-17 11:24:54 +00:00
|
|
|
return <Trans>form_error_ip_format</Trans>;
|
2018-12-12 15:12:51 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-12-13 12:26:47 +00:00
|
|
|
const isPositive = (value) => {
|
|
|
|
if ((value || value === 0) && (value <= 0)) {
|
2018-12-17 11:24:54 +00:00
|
|
|
return <Trans>form_error_positive</Trans>;
|
2018-12-13 12:26:47 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const toNumber = value => value && parseInt(value, 10);
|
|
|
|
|
2018-12-12 15:12:51 +00:00
|
|
|
const renderField = ({
|
|
|
|
input, className, placeholder, type, disabled, meta: { touched, error },
|
|
|
|
}) => (
|
|
|
|
<Fragment>
|
|
|
|
<input
|
|
|
|
{...input}
|
|
|
|
placeholder={placeholder}
|
|
|
|
type={type}
|
|
|
|
className={className}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
{!disabled && touched && (error && <span className="form__message form__message--error">{error}</span>)}
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
2018-12-24 08:48:23 +00:00
|
|
|
const renderInterfaces = (interfaces => (
|
|
|
|
Object.keys(interfaces).map((item) => {
|
|
|
|
const option = interfaces[item];
|
|
|
|
const { name } = option;
|
2018-12-28 16:48:02 +00:00
|
|
|
const onlyIPv6 = option.ip_addresses.every(ip => ip.includes(':'));
|
2018-12-24 08:48:23 +00:00
|
|
|
let interfaceIP = option.ip_addresses[0];
|
|
|
|
|
2018-12-28 16:48:02 +00:00
|
|
|
if (!onlyIPv6) {
|
|
|
|
option.ip_addresses.forEach((ip) => {
|
|
|
|
if (!ip.includes(':')) {
|
|
|
|
interfaceIP = ip;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-12-24 08:48:23 +00:00
|
|
|
|
|
|
|
return (
|
2018-12-28 16:48:02 +00:00
|
|
|
<option value={name} key={name} disabled={onlyIPv6}>
|
2018-12-24 08:48:23 +00:00
|
|
|
{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>
|
|
|
|
));
|
|
|
|
|
|
|
|
let Form = (props) => {
|
2018-12-12 15:12:51 +00:00
|
|
|
const {
|
2018-12-24 08:48:23 +00:00
|
|
|
t,
|
|
|
|
handleSubmit,
|
|
|
|
pristine,
|
|
|
|
submitting,
|
|
|
|
interfaces,
|
|
|
|
processing,
|
|
|
|
interfaceValue,
|
2018-12-12 15:12:51 +00:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<div className="row">
|
2018-12-24 08:48:23 +00:00
|
|
|
<div className="col-12">
|
|
|
|
{!processing && interfaces &&
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-sm-12 col-md-6">
|
|
|
|
<div className="form__group form__group--dhcp">
|
|
|
|
<label>{t('dhcp_interface_select')}</label>
|
2018-12-28 16:48:02 +00:00
|
|
|
<Field
|
|
|
|
name="interface_name"
|
|
|
|
component="select"
|
|
|
|
className="form-control custom-select"
|
|
|
|
>
|
2018-12-24 08:48:23 +00:00
|
|
|
<option value="">{t('dhcp_interface_select')}</option>
|
|
|
|
{renderInterfaces(interfaces)}
|
|
|
|
</Field>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{interfaceValue &&
|
|
|
|
<div className="col-sm-12 col-md-6">
|
|
|
|
{renderInterfaceValues(interfaces[interfaceValue])}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
<hr/>
|
|
|
|
</div>
|
2018-12-12 15:12:51 +00:00
|
|
|
<div className="col-lg-6">
|
|
|
|
<div className="form__group form__group--dhcp">
|
2018-12-17 11:24:54 +00:00
|
|
|
<label>{t('dhcp_form_gateway_input')}</label>
|
2018-12-12 15:12:51 +00:00
|
|
|
<Field
|
|
|
|
name="gateway_ip"
|
|
|
|
component={renderField}
|
|
|
|
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>
|
|
|
|
<div className="form__group form__group--dhcp">
|
2018-12-17 11:24:54 +00:00
|
|
|
<label>{t('dhcp_form_subnet_input')}</label>
|
2018-12-12 15:12:51 +00:00
|
|
|
<Field
|
|
|
|
name="subnet_mask"
|
|
|
|
component={renderField}
|
|
|
|
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">
|
|
|
|
<div className="form__group form__group--dhcp">
|
|
|
|
<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
|
|
|
|
name="range_start"
|
|
|
|
component={renderField}
|
|
|
|
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
|
|
|
|
name="range_end"
|
|
|
|
component={renderField}
|
|
|
|
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>
|
|
|
|
<div className="form__group form__group--dhcp">
|
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"
|
|
|
|
component={renderField}
|
|
|
|
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>
|
|
|
|
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-success btn-standart"
|
2018-12-28 16:48:02 +00:00
|
|
|
disabled={pristine || submitting}
|
2018-12-12 15:12:51 +00:00
|
|
|
>
|
2018-12-17 11:24:54 +00:00
|
|
|
{t('save_config')}
|
2018-12-12 15:12:51 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Form.propTypes = {
|
|
|
|
handleSubmit: PropTypes.func,
|
|
|
|
pristine: PropTypes.bool,
|
|
|
|
submitting: PropTypes.bool,
|
2018-12-24 08:48:23 +00:00
|
|
|
interfaces: PropTypes.object,
|
|
|
|
processing: PropTypes.bool,
|
|
|
|
interfaceValue: PropTypes.string,
|
2018-12-28 16:48:02 +00:00
|
|
|
initialValues: PropTypes.object,
|
2018-12-17 11:24:54 +00:00
|
|
|
t: PropTypes.func,
|
2018-12-12 15:12:51 +00:00
|
|
|
};
|
|
|
|
|
2018-12-24 08:48:23 +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);
|