badguardhome/client/src/install/Setup/Settings.js

222 lines
7.4 KiB
JavaScript
Raw Normal View History

2019-01-18 17:17:48 +00:00
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Field, reduxForm, formValueSelector } from 'redux-form';
2019-01-22 14:54:18 +00:00
import { Trans, withNamespaces } from 'react-i18next';
import flow from 'lodash/flow';
2019-01-18 17:17:48 +00:00
import Controls from './Controls';
2019-02-04 14:13:59 +00:00
import AddressList from './AddressList';
2019-01-18 17:17:48 +00:00
import renderField from './renderField';
2019-02-04 14:13:59 +00:00
import { getInterfaceIp } from '../../helpers/helpers';
2019-01-18 17:17:48 +00:00
const required = (value) => {
if (value || value === 0) {
return false;
}
return <Trans>form_error_required</Trans>;
};
const port = (value) => {
if (value < 1 || value > 65535) {
return <Trans>form_error_port</Trans>;
}
return false;
};
const toNumber = value => value && parseInt(value, 10);
2019-02-01 16:52:42 +00:00
const renderInterfaces = (interfaces => (
Object.keys(interfaces).map((item) => {
const option = interfaces[item];
const { name } = option;
if (option.ip_addresses && option.ip_addresses.length > 0) {
const ip = getInterfaceIp(option);
return (
<option value={ip} key={name}>
{name} - {ip}
</option>
);
}
return false;
2019-02-01 16:52:42 +00:00
})
));
2019-01-18 17:17:48 +00:00
let Settings = (props) => {
const {
handleSubmit,
2019-02-04 14:13:59 +00:00
webIp,
webPort,
2019-01-18 17:17:48 +00:00
dnsIp,
2019-01-31 09:16:08 +00:00
dnsPort,
2019-02-01 16:52:42 +00:00
interfaces,
2019-01-21 08:55:39 +00:00
invalid,
2019-02-01 16:52:42 +00:00
webWarning,
dnsWarning,
2019-01-18 17:17:48 +00:00
} = props;
return (
<form className="setup__step" onSubmit={handleSubmit}>
<div className="setup__group">
<div className="setup__subtitle">
<Trans>install_settings_title</Trans>
</div>
<div className="row">
<div className="col-8">
<div className="form-group">
<label>
<Trans>install_settings_listen</Trans>
</label>
<Field
name="web.ip"
2019-02-01 16:52:42 +00:00
component="select"
className="form-control custom-select"
>
<option value="0.0.0.0">
<Trans>install_settings_all_interfaces</Trans>
</option>
{renderInterfaces(interfaces)}
</Field>
2019-01-18 17:17:48 +00:00
</div>
</div>
<div className="col-4">
<div className="form-group">
<label>
<Trans>install_settings_port</Trans>
</label>
<Field
name="web.port"
component={renderField}
type="number"
className="form-control"
placeholder="80"
validate={[port, required]}
normalize={toNumber}
/>
</div>
</div>
</div>
<div className="setup__desc">
2019-02-04 14:13:59 +00:00
<Trans>install_settings_interface_link</Trans>
<div className="mt-1">
<AddressList
interfaces={interfaces}
address={webIp}
port={webPort}
/>
</div>
2019-02-01 16:52:42 +00:00
{webWarning &&
<div className="text-danger mt-2">
{webWarning}
</div>
}
2019-01-18 17:17:48 +00:00
</div>
</div>
<div className="setup__group">
<div className="setup__subtitle">
<Trans>install_settings_dns</Trans>
</div>
<div className="row">
<div className="col-8">
<div className="form-group">
<label>
<Trans>install_settings_listen</Trans>
</label>
<Field
name="dns.ip"
2019-02-01 16:52:42 +00:00
component="select"
className="form-control custom-select"
>
2019-02-04 14:13:59 +00:00
<option value="0.0.0.0">
2019-02-01 16:52:42 +00:00
<Trans>install_settings_all_interfaces</Trans>
</option>
{renderInterfaces(interfaces)}
</Field>
2019-01-18 17:17:48 +00:00
</div>
</div>
<div className="col-4">
<div className="form-group">
<label>
<Trans>install_settings_port</Trans>
</label>
<Field
name="dns.port"
component={renderField}
type="number"
className="form-control"
placeholder="80"
validate={[port, required]}
normalize={toNumber}
/>
</div>
</div>
</div>
2019-02-01 16:52:42 +00:00
<div className="setup__desc">
2019-02-04 14:13:59 +00:00
<Trans>install_settings_dns_desc</Trans>
<div className="mt-1">
<AddressList
interfaces={interfaces}
address={dnsIp}
port={dnsPort}
isDns={true}
/>
</div>
2019-02-01 16:52:42 +00:00
{dnsWarning &&
<div className="text-danger mt-2">
{dnsWarning}
</div>
}
</div>
2019-01-18 17:17:48 +00:00
</div>
2019-01-21 08:55:39 +00:00
<Controls invalid={invalid} />
2019-01-18 17:17:48 +00:00
</form>
);
};
Settings.propTypes = {
handleSubmit: PropTypes.func.isRequired,
2019-02-04 14:13:59 +00:00
webIp: PropTypes.string.isRequired,
2019-01-18 17:17:48 +00:00
dnsIp: PropTypes.string.isRequired,
2019-02-04 14:13:59 +00:00
webPort: PropTypes.oneOfType([
2019-01-31 09:16:08 +00:00
PropTypes.string,
PropTypes.number,
]),
dnsPort: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
2019-02-01 16:52:42 +00:00
webWarning: PropTypes.string.isRequired,
dnsWarning: PropTypes.string.isRequired,
interfaces: PropTypes.object.isRequired,
2019-01-21 08:55:39 +00:00
invalid: PropTypes.bool.isRequired,
2019-01-18 17:17:48 +00:00
initialValues: PropTypes.object,
};
const selector = formValueSelector('install');
Settings = connect((state) => {
2019-02-04 14:13:59 +00:00
const webIp = selector(state, 'web.ip');
const webPort = selector(state, 'web.port');
2019-01-18 17:17:48 +00:00
const dnsIp = selector(state, 'dns.ip');
2019-01-31 09:16:08 +00:00
const dnsPort = selector(state, 'dns.port');
2019-01-18 17:17:48 +00:00
return {
2019-02-04 14:13:59 +00:00
webIp,
webPort,
2019-01-18 17:17:48 +00:00
dnsIp,
2019-01-31 09:16:08 +00:00
dnsPort,
2019-01-18 17:17:48 +00:00
};
})(Settings);
2019-01-22 14:54:18 +00:00
export default flow([
withNamespaces(),
reduxForm({
form: 'install',
destroyOnUnmount: false,
forceUnregisterOnUnmount: true,
}),
])(Settings);