import React from 'react'; import './ConfigurationItem.css'; import CheckBox from '../../../components/UI/CheckBox/CheckBox'; import DropDown from '../../../components/UI/DropDown/DropDown'; import HostList from '../../HostList/HostList'; import Password from '../../../containers/UI/Password/Password'; import PropTypes from 'prop-types'; import settings from '../../../assets/settings'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { connect } from 'react-redux'; import { faInfoCircle } from '@fortawesome/free-solid-svg-icons'; import { notifyError, notifyInfo } from '../../../redux/actions/error_actions'; const ConfigurationItem = (props) => { const handleChanged = (e) => { const target = e.target; if (target.type === 'checkbox') { target.value = e.target.checked ? 'true' : 'false'; } else if (target.type === 'textarea') { e.target.string_array = String(e.target.value).replace(/\r\n/g, '\n').split('\n'); } props.changed(target); }; let infoDisplay; if (settings[props.grouping] && settings[props.grouping][props.label]) { const displayInfo = () => { const description = settings[props.grouping][props.label]; props.notifyInfo(props.label, description); }; infoDisplay = ( { displayInfo(); return false; }}> ); } let data; switch (props.template.type) { case 'bool': data = ( ); break; case 'double': data = ( handleChanged(e)} step={'0.01'} className={'ConfigurationItemInput'} type={'number'} value={parseFloat(props.value).toFixed(2)} /> ); break; case 'host_list': data = ( handleChanged({ target: { type: 'host_list', value: items, }, }) } type={props.template.subtype} value={props.value} /> ); break; case 'list': data = ( ); break; case 'string': if (props.template.subtype === 'password') { data = ( handleChanged({ target: { type: 'password', value: s, }, }) } disabled={props.readOnly} mismatchHandler={() => props.notifyError('Passwords do not match')} value={props.value} /> ); } else { data = ( handleChanged(e)} autoFocus={props.autoFocus} className={'ConfigurationItemInput'} disabled={props.readOnly} type={'text'} value={props.value} /> ); } break; case 'uint8': data = ( handleChanged(e)} className={'ConfigurationItemInput'} type={'number'} value={props.value} /> ); break; case 'uint16': data = ( handleChanged(e)} className={'ConfigurationItemInput'} type={'number'} value={props.value} /> ); break; case 'uint32': data = ( handleChanged(e)} className={'ConfigurationItemInput'} type={'number'} value={props.value} /> ); break; case 'uint64': data = ( handleChanged(e)} className={'ConfigurationItemInput'} type={'number'} value={props.value} /> ); break; case 'string_array': data = ( handleChanged(e)} className={'ConfigurationItemInput'} value={props.value.join('\n')} /> ); break; case 'password': data = ( handleChanged({ target: { type: 'password', value: s, }, }) } disabled={props.readOnly} mismatchHandler={() => props.notifyError('Passwords do not match')} value={props.value} /> ); break; default: data = {props.value}; } return ( {infoDisplay ? ( {infoDisplay} {props.label} ) : ( {props.label} )} {data} ); }; const mapDispatchToProps = (dispatch) => { return { notifyError: (msg) => dispatch(notifyError(msg)), notifyInfo: (title, msg) => dispatch(notifyInfo(title, msg)), }; }; ConfigurationItem.propTypes = { autoFocus: PropTypes.bool, changed: PropTypes.func.isRequired, grouping: PropTypes.string, items: PropTypes.array, label: PropTypes.string, notifyError: PropTypes.func.isRequired, notifyInfo: PropTypes.func.isRequired, readOnly: PropTypes.bool, template: PropTypes.object.isRequired, value: PropTypes.oneOfType([PropTypes.array, PropTypes.string, PropTypes.number]), }; export default connect(null, mapDispatchToProps)(ConfigurationItem);