Updates #4695. Updates #5190. Squashed commit of the following: commit f4156f54ebd1b0c96318715441da1dc54cbbfb0f Author: Ildar Kamalov <ik@adguard.com> Date: Fri Dec 2 16:19:03 2022 +0300 client: button styles commit 42cd8ac8ac1abb5c43a42065e2374d11f2d9a62b Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Dec 2 15:39:15 2022 +0300 all: upd dnsproxy commit 87b04391afeaed28523448aa12a317300e718b72 Author: Ildar Kamalov <ik@adguard.com> Date: Fri Dec 2 15:19:07 2022 +0300 client: add clear cache button commit ee99548b3689ce47959d9bddc6a1e866e773dcc3 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Dec 1 18:54:54 2022 +0300 all: add cache clear
126 lines
4.6 KiB
JavaScript
126 lines
4.6 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, useDispatch, 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';
|
|
import { clearDnsCache } from '../../../../actions/dnsConfig';
|
|
|
|
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 dispatch = useDispatch();
|
|
|
|
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;
|
|
|
|
const handleClearCache = () => {
|
|
if (window.confirm(t('confirm_dns_cache_clear'))) {
|
|
dispatch(clearDnsCache());
|
|
}
|
|
};
|
|
|
|
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>
|
|
<button
|
|
type="button"
|
|
className="btn btn-outline-secondary btn-standard form__button"
|
|
onClick={handleClearCache}
|
|
>
|
|
<Trans>clear_cache</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);
|