Updates #2145. Squashed commit of the following: commit 0c15347f4573252849817f27f290c0d45381454c Merge: 98bd3b89ebade2b6Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Jul 14 20:44:58 2021 +0300 Merge branch 'master' into 2145-optimistic-cache commit 98bd3b895e0d881d5234f674b54f9fa7847dc8f0 Author: Ildar Kamalov <ik@adguard.com> Date: Wed Jul 14 13:12:56 2021 +0300 client: handle optimistic cache commit 0b469b72ffd43d736dbf139e7d47b23b9fa877c5 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Jul 1 19:01:01 2021 +0300 openapi: fix log of changes commit f1594e7f7567e0278b08025a8e4da901ef330602 Merge: a034eb98e113b276Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Jul 1 18:53:01 2021 +0300 Merge branch 'master' into 2145-optimistic-cache commit a034eb98bafdca90befad7dfb6a9b0e4c939c879 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jun 29 18:45:28 2021 +0300 dnsforward: fix tests commit c72227f83c849714721c3512beeb9d1b800a7225 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jun 29 18:33:12 2021 +0300 openapi: imp docs commit 35fe0d2a8c98d007b8ac48653c18d10d52e72dce Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jun 28 14:19:46 2021 +0300 dnsforward: add optimistic cache
110 lines
4.1 KiB
JavaScript
110 lines
4.1 KiB
JavaScript
import React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { Field, reduxForm } from 'redux-form';
|
|
import { Trans, useTranslation } from 'react-i18next';
|
|
import { shallowEqual, useSelector } from 'react-redux';
|
|
import { renderInputField, toNumber, CheckboxField } from '../../../../helpers/form';
|
|
import { CACHE_CONFIG_FIELDS, FORM_NAME, UINT32_RANGE } from '../../../../helpers/constants';
|
|
import { replaceZeroWithEmptyString } from '../../../../helpers/helpers';
|
|
|
|
const INPUTS_FIELDS = [
|
|
{
|
|
name: CACHE_CONFIG_FIELDS.cache_size,
|
|
title: 'cache_size',
|
|
description: 'cache_size_desc',
|
|
placeholder: 'enter_cache_size',
|
|
},
|
|
{
|
|
name: CACHE_CONFIG_FIELDS.cache_ttl_min,
|
|
title: 'cache_ttl_min_override',
|
|
description: 'cache_ttl_min_override_desc',
|
|
placeholder: 'enter_cache_ttl_min_override',
|
|
},
|
|
{
|
|
name: CACHE_CONFIG_FIELDS.cache_ttl_max,
|
|
title: 'cache_ttl_max_override',
|
|
description: 'cache_ttl_max_override_desc',
|
|
placeholder: 'enter_cache_ttl_max_override',
|
|
},
|
|
];
|
|
|
|
const Form = ({
|
|
handleSubmit, submitting, invalid,
|
|
}) => {
|
|
const { t } = useTranslation();
|
|
|
|
const { processingSetConfig } = useSelector((state) => state.dnsConfig, shallowEqual);
|
|
const {
|
|
cache_ttl_max, cache_ttl_min,
|
|
} = useSelector((state) => state.form[FORM_NAME.CACHE].values, shallowEqual);
|
|
|
|
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}
|
|
className="form-control"
|
|
validate={validate}
|
|
normalizeOnBlur={replaceZeroWithEmptyString}
|
|
normalize={toNumber}
|
|
min={min}
|
|
max={max}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>)}
|
|
{minExceedsMax && (
|
|
<span className="text-danger pl-3 pb-3">
|
|
{t('ttl_cache_validation')}
|
|
</span>
|
|
)}
|
|
</div>
|
|
<div className="row">
|
|
<div className="col-12 col-md-7">
|
|
<div className="form__group form__group--settings">
|
|
<Field
|
|
name="cache_optimistic"
|
|
type="checkbox"
|
|
component={CheckboxField}
|
|
placeholder={t('cache_optimistic')}
|
|
disabled={processingSetConfig}
|
|
subtitle={t('cache_optimistic_desc')}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<button
|
|
type="submit"
|
|
className="btn btn-success btn-standard btn-large"
|
|
disabled={submitting || invalid || processingSetConfig || minExceedsMax}
|
|
>
|
|
<Trans>save_btn</Trans>
|
|
</button>
|
|
</form>;
|
|
};
|
|
|
|
Form.propTypes = {
|
|
handleSubmit: PropTypes.func.isRequired,
|
|
submitting: PropTypes.bool.isRequired,
|
|
invalid: PropTypes.bool.isRequired,
|
|
};
|
|
|
|
export default reduxForm({ form: FORM_NAME.CACHE })(Form);
|