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 { renderInputField, required, ipv4, isPositive, toNumber } from '../../../helpers/form'; 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 => ( )); 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(); } }; let Form = (props) => { const { t, handleSubmit, submitting, invalid, enabled, interfaces, interfaceValue, processingConfig, processingInterfaces, resetDhcp, change, } = props; return (
{!processingInterfaces && interfaces &&
{renderInterfaces(interfaces)}
{interfaceValue &&
{interfaces[interfaceValue] && renderInterfaceValues(interfaces[interfaceValue])}
}
}
); }; Form.propTypes = { handleSubmit: PropTypes.func.isRequired, submitting: PropTypes.bool.isRequired, invalid: PropTypes.bool.isRequired, interfaces: PropTypes.object.isRequired, interfaceValue: PropTypes.string, 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, }; const selector = formValueSelector('dhcpForm'); Form = connect((state) => { const interfaceValue = selector(state, 'interface_name'); return { interfaceValue, }; })(Form); export default flow([ withNamespaces(), reduxForm({ form: 'dhcpForm' }), ])(Form);