badguardhome/client/src/components/Settings/Dns/Upstream/Form.js
Artem Baskal 0ffc0965dc + client: add fastest_addr setting
Squashed commit of the following:

commit e47fae25f7bac950bfb452fc8f18b9c0865b08ba
Merge: a23285ec e2ddc82d
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Wed Apr 22 19:16:01 2020 +0300

    Merge remote-tracking branch 'origin/master' into 715

commit a23285ec3ace78fe4ce19122a51ecf3e6cdd942c
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Wed Apr 22 18:30:30 2020 +0300

    Review changes

commit f80d62a0d2038ff9d070ae9e9c77c33b92232d9c
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Apr 21 16:37:42 2020 +0300

    + client: Add fastest addr option

commit 9e713df80c5bf113c98794c0a20915c756a76938
Merge: e3bf4037 9b7c1181
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Apr 21 16:02:03 2020 +0300

    Merge remote-tracking branch 'origin/master' into 715-fastest-addr

commit e3bf4037f49198e42bde55305d6f9077341b556a
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Apr 21 15:40:49 2020 +0300

    minor

commit d6e6a823c5e51acc061b2850d362772efcb827e1
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Fri Apr 17 17:56:24 2020 +0300

    * API changes

    . removed POST /set_upstreams_config
    . removed fields from GET /status: bootstrap_dns, upstream_dns, all_servers
    . added new fields to /dns_config and /dns_info

commit 237a452d09cc48ff8f00e81c7fd35e7828bea835
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Fri Apr 17 16:43:13 2020 +0300

    * API: /dns_info, /dns_config: add "parallel_requests" instead of "all_servers" from /set_upstreams_config

commit 9976723b9725ed19e0cce152d1d1198b13c4acc1
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Mon Mar 23 10:28:25 2020 +0300

    openapi

commit 6f8ea16c6332606f29095b0094d71e8a91798f82
Merge: 36e4d4e8 c8285c41
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Fri Mar 20 19:18:48 2020 +0300

    Merge remote-tracking branch 'origin/master' into 715-fastest-addr

commit 36e4d4e82cadeaba5a11313f0d69d66a0924c342
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Fri Mar 20 18:13:43 2020 +0300

    + DNS: add fastest_addr setting
2020-04-22 19:32:07 +03:00

172 lines
5.6 KiB
JavaScript

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 classnames from 'classnames';
import Examples from './Examples';
import { renderSelectField } from '../../../../helpers/form';
const getInputFields = (parallel_requests_selected, fastest_addr_selected) => [{
// eslint-disable-next-line react/display-name
getTitle: () => <label className="form__label" htmlFor="upstream_dns">
<Trans>upstream_dns</Trans>
</label>,
name: 'upstream_dns',
type: 'text',
component: 'textarea',
className: 'form-control form-control--textarea',
placeholder: 'upstream_dns',
},
{
name: 'parallel_requests',
placeholder: 'parallel_requests',
component: renderSelectField,
type: 'checkbox',
subtitle: 'upstream_parallel',
disabled: fastest_addr_selected,
},
{
name: 'fastest_addr',
placeholder: 'fastest_addr',
component: renderSelectField,
type: 'checkbox',
subtitle: 'fastest_addr_desc',
disabled: parallel_requests_selected,
}];
let Form = (props) => {
const {
t,
handleSubmit,
testUpstream,
submitting,
invalid,
processingSetConfig,
processingTestUpstream,
fastest_addr,
parallel_requests,
upstream_dns,
bootstrap_dns,
} = props;
const testButtonClass = classnames({
'btn btn-primary btn-standard mr-2': true,
'btn btn-primary btn-standard mr-2 btn-loading': processingTestUpstream,
});
const INPUT_FIELDS = getInputFields(parallel_requests, fastest_addr);
return (
<form onSubmit={handleSubmit}>
<div className="row">
{INPUT_FIELDS.map(({
name, component, type, className, placeholder, getTitle, subtitle, disabled,
}) => <div className="col-12 mb-4" key={name}>
{typeof getTitle === 'function' && getTitle()}
<Field
id={name}
name={name}
component={component}
type={type}
className={className}
placeholder={t(placeholder)}
subtitle={t(subtitle)}
disabled={processingSetConfig || processingTestUpstream || disabled}
/>
</div>)}
<div className="col-12">
<Examples />
<hr />
</div>
<div className="col-12 mb-4">
<label
className="form__label form__label--with-desc"
htmlFor="bootstrap_dns"
>
<Trans>bootstrap_dns</Trans>
</label>
<div className="form__desc form__desc--top">
<Trans>bootstrap_dns_desc</Trans>
</div>
<Field
id="bootstrap_dns"
name="bootstrap_dns"
component="textarea"
type="text"
className="form-control form-control--textarea form-control--textarea-small"
placeholder={t('bootstrap_dns')}
disabled={processingSetConfig}
/>
</div>
</div>
<div className="card-actions">
<div className="btn-list">
<button
type="button"
className={testButtonClass}
onClick={() =>
testUpstream({
upstream_dns,
bootstrap_dns,
})
}
disabled={!upstream_dns || processingTestUpstream}
>
<Trans>test_upstream_btn</Trans>
</button>
<button
type="submit"
className="btn btn-success btn-standard"
disabled={
submitting || invalid || processingSetConfig || processingTestUpstream
}
>
<Trans>apply_btn</Trans>
</button>
</div>
</div>
</form>
);
};
Form.propTypes = {
handleSubmit: PropTypes.func,
testUpstream: PropTypes.func,
submitting: PropTypes.bool,
invalid: PropTypes.bool,
initialValues: PropTypes.object,
upstream_dns: PropTypes.string,
bootstrap_dns: PropTypes.string,
fastest_addr: PropTypes.bool,
parallel_requests: PropTypes.bool,
processingTestUpstream: PropTypes.bool,
processingSetConfig: PropTypes.bool,
t: PropTypes.func,
};
const selector = formValueSelector('upstreamForm');
Form = connect((state) => {
const upstream_dns = selector(state, 'upstream_dns');
const bootstrap_dns = selector(state, 'bootstrap_dns');
const fastest_addr = selector(state, 'fastest_addr');
const parallel_requests = selector(state, 'parallel_requests');
return {
upstream_dns,
bootstrap_dns,
fastest_addr,
parallel_requests,
};
})(Form);
export default flow([
withNamespaces(),
reduxForm({
form: 'upstreamForm',
}),
])(Form);