import React, { Fragment } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Field, reduxForm, formValueSelector } from 'redux-form';
import { withNamespaces, Trans } from 'react-i18next';
import flow from 'lodash/flow';
import { R_IPV4 } from '../../../helpers/constants';
const required = (value) => {
if (value || value === 0) {
return false;
}
return form_error_required;
};
const ipv4 = (value) => {
if (value && !new RegExp(R_IPV4).test(value)) {
return form_error_ip_format;
}
return false;
};
const isPositive = (value) => {
if ((value || value === 0) && (value <= 0)) {
return form_error_positive;
}
return false;
};
const toNumber = value => value && parseInt(value, 10);
const renderField = ({
input, className, placeholder, type, disabled, meta: { touched, error },
}) => (
{!disabled && touched && (error && {error})}
);
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 (
);
})
));
const renderInterfaceValues = (interfaceValues => (
-
MTU:
{interfaceValues.mtu}
-
dhcp_hardware_address:
{interfaceValues.hardware_address}
-
dhcp_ip_addresses:
{
interfaceValues.ip_addresses
.map(ip => {ip})
}
));
let Form = (props) => {
const {
t,
handleSubmit,
pristine,
submitting,
interfaces,
processing,
interfaceValue,
} = props;
return (
);
};
Form.propTypes = {
handleSubmit: PropTypes.func,
pristine: PropTypes.bool,
submitting: PropTypes.bool,
interfaces: PropTypes.object,
processing: PropTypes.bool,
interfaceValue: PropTypes.string,
initialValues: PropTypes.object,
t: PropTypes.func,
};
const selector = formValueSelector('dhcpForm');
Form = connect((state) => {
const interfaceValue = selector(state, 'interface_name');
return {
interfaceValue,
};
})(Form);
export default flow([
withNamespaces(),
reduxForm({ form: 'dhcpForm' }),
])(Form);