import React from 'react'; import { connect } from 'react-redux'; import PropTypes from 'prop-types'; import { Field, reduxForm, formValueSelector } from 'redux-form'; import { Trans, withNamespaces } from 'react-i18next'; import flow from 'lodash/flow'; import Controls from './Controls'; import renderField from './renderField'; const required = (value) => { if (value || value === 0) { return false; } return form_error_required; }; const port = (value) => { if (value < 1 || value > 65535) { return form_error_port; } return false; }; const toNumber = value => value && parseInt(value, 10); 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 ( ); }) )); let Settings = (props) => { const { handleSubmit, interfaceIp, interfacePort, dnsIp, dnsPort, interfaces, invalid, webWarning, dnsWarning, } = props; const dnsAddress = dnsPort && dnsPort !== 53 ? `${dnsIp}:${dnsPort}` : dnsIp; const interfaceAddress = interfacePort ? `http://${interfaceIp}:${interfacePort}` : `http://${interfaceIp}`; return (
install_settings_title
{renderInterfaces(interfaces)}
link]} values={{ link: interfaceAddress }} > install_settings_interface_link {webWarning &&
{webWarning}
}
install_settings_dns
{renderInterfaces(interfaces)}
ip]} values={{ ip: dnsAddress }} > install_settings_dns_desc {dnsWarning &&
{dnsWarning}
}
); }; Settings.propTypes = { handleSubmit: PropTypes.func.isRequired, interfaceIp: PropTypes.string.isRequired, dnsIp: PropTypes.string.isRequired, interfacePort: PropTypes.oneOfType([ PropTypes.string, PropTypes.number, ]), dnsPort: PropTypes.oneOfType([ PropTypes.string, PropTypes.number, ]), webWarning: PropTypes.string.isRequired, dnsWarning: PropTypes.string.isRequired, interfaces: PropTypes.object.isRequired, invalid: PropTypes.bool.isRequired, initialValues: PropTypes.object, }; const selector = formValueSelector('install'); Settings = connect((state) => { const interfaceIp = selector(state, 'web.ip'); const interfacePort = selector(state, 'web.port'); const dnsIp = selector(state, 'dns.ip'); const dnsPort = selector(state, 'dns.port'); return { interfaceIp, interfacePort, dnsIp, dnsPort, }; })(Settings); export default flow([ withNamespaces(), reduxForm({ form: 'install', destroyOnUnmount: false, forceUnregisterOnUnmount: true, }), ])(Settings);