+ client: Replace TTL override zeroes with empty strings, update dnsproxy

Close #2091

Squashed commit of the following:

commit 9acaa0331f3094a2b48c1f0cf846a30e5c9dfc43
Merge: c207ccc9 3a71374f
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Fri Sep 18 17:45:20 2020 +0300

    Merge branch 'master' into fix/2091

commit c207ccc9a12f225a6f0343f5273a3469b2709679
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Fri Sep 18 17:14:24 2020 +0300

    Remove unused imports

commit 750f60ffc40fd48d4bd09a4e2ac4a3143e0cf126
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Fri Sep 18 16:47:24 2020 +0300

    Update dnsproxy to v0.32.6

commit 1723267e0e7f512db532cd180a3e9d44182d96b3
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Fri Sep 18 16:32:12 2020 +0300

    Replace zeroes with empty string
This commit is contained in:
Artem Baskal
2020-09-21 10:19:56 +03:00
parent 3a71374f9f
commit bf23aa4d9b
5 changed files with 82 additions and 55 deletions

View File

@@ -4,16 +4,15 @@ import { Field, reduxForm } from 'redux-form';
import { Trans, useTranslation } from 'react-i18next';
import { shallowEqual, useSelector } from 'react-redux';
import { renderInputField, toNumber } from '../../../../helpers/form';
import { validateRequiredValue } from '../../../../helpers/validators';
import { CACHE_CONFIG_FIELDS, FORM_NAME, UINT32_RANGE } from '../../../../helpers/constants';
import { replaceZeroWithEmptyString } from '../../../../helpers/helpers';
const getInputFields = (validateRequiredValue) => [
const INPUTS_FIELDS = [
{
name: CACHE_CONFIG_FIELDS.cache_size,
title: 'cache_size',
description: 'cache_size_desc',
placeholder: 'enter_cache_size',
validate: validateRequiredValue,
},
{
name: CACHE_CONFIG_FIELDS.cache_ttl_min,
@@ -39,37 +38,34 @@ const Form = ({
cache_ttl_max, cache_ttl_min,
} = useSelector((state) => state.form[FORM_NAME.CACHE].values, shallowEqual);
const minExceedsMax = typeof cache_ttl_min === 'number'
&& typeof cache_ttl_max === 'number'
&& cache_ttl_min > cache_ttl_max;
const INPUTS_FIELDS = getInputFields(validateRequiredValue);
const minExceedsMax = cache_ttl_min > cache_ttl_max;
return <form onSubmit={handleSubmit}>
<div className="row">
{INPUTS_FIELDS.map(({
name, title, description, placeholder, validate, min = 0, max = UINT32_RANGE.MAX,
}) => <div className="col-12" key={name}>
<div className="col-12 col-md-7 p-0">
<div className="form__group form__group--settings">
<label htmlFor={name}
className="form__label form__label--with-desc">{t(title)}</label>
<div className="form__desc form__desc--top">{t(description)}</div>
<Field
name={name}
type="number"
component={renderInputField}
placeholder={t(placeholder)}
disabled={processingSetConfig}
normalize={toNumber}
className="form-control"
validate={validate}
min={min}
max={max}
/>
<div className="col-12 col-md-7 p-0">
<div className="form__group form__group--settings">
<label htmlFor={name}
className="form__label form__label--with-desc">{t(title)}</label>
<div className="form__desc form__desc--top">{t(description)}</div>
<Field
name={name}
type="number"
component={renderInputField}
placeholder={t(placeholder)}
disabled={processingSetConfig}
className="form-control"
validate={validate}
normalizeOnBlur={replaceZeroWithEmptyString}
normalize={toNumber}
min={min}
max={max}
/>
</div>
</div>
</div>
</div>)}
</div>)}
{minExceedsMax
&& <span className="text-danger pl-3 pb-3">{t('ttl_cache_validation')}</span>}
</div>

View File

@@ -1,12 +1,10 @@
import React from 'react';
import { useTranslation } from 'react-i18next';
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
import { change } from 'redux-form';
import Card from '../../../ui/Card';
import Form from './Form';
import { setDnsConfig } from '../../../../actions/dnsConfig';
import { selectCompletedFields } from '../../../../helpers/helpers';
import { CACHE_CONFIG_FIELDS, FORM_NAME } from '../../../../helpers/constants';
import { replaceEmptyStringsWithZeroes, replaceZeroWithEmptyString } from '../../../../helpers/helpers';
const CacheConfig = () => {
const { t } = useTranslation();
@@ -16,16 +14,7 @@ const CacheConfig = () => {
} = useSelector((state) => state.dnsConfig, shallowEqual);
const handleFormSubmit = (values) => {
const completedFields = selectCompletedFields(values);
Object.entries(completedFields).forEach(([k, v]) => {
if ((k === CACHE_CONFIG_FIELDS.cache_ttl_min
|| k === CACHE_CONFIG_FIELDS.cache_ttl_max)
&& v === 0) {
dispatch(change(FORM_NAME.CACHE, k, ''));
}
});
const completedFields = replaceEmptyStringsWithZeroes(values);
dispatch(setDnsConfig(completedFields));
};
@@ -39,9 +28,9 @@ const CacheConfig = () => {
<div className="form">
<Form
initialValues={{
cache_size,
cache_ttl_max,
cache_ttl_min,
cache_size: replaceZeroWithEmptyString(cache_size),
cache_ttl_max: replaceZeroWithEmptyString(cache_ttl_max),
cache_ttl_min: replaceZeroWithEmptyString(cache_ttl_min),
}}
onSubmit={handleFormSubmit}
/>

View File

@@ -717,14 +717,18 @@ export const setHtmlLangAttr = (language) => {
* @param values {object}
* @returns {object}
*/
export const selectCompletedFields = (values) => Object.entries(values)
export const replaceEmptyStringsWithZeroes = (values) => Object.entries(values)
.reduce((acc, [key, value]) => {
if (value || value === 0) {
acc[key] = value;
}
acc[key] = value === '' ? 0 : value;
return acc;
}, {});
/**
* @param value {number || string}
* @returns {string}
*/
export const replaceZeroWithEmptyString = (value) => (parseInt(value, 10) === 0 ? '' : value);
/**
* @param {string} search
* @param {string} [response_status]