stats config form

This commit is contained in:
Ildar Kamalov
2025-01-14 15:17:00 +03:00
parent d0dc0c600d
commit 8b40fe97a6
5 changed files with 181 additions and 134 deletions

View File

@@ -10,6 +10,7 @@ import {
DAY, DAY,
RETENTION_CUSTOM, RETENTION_CUSTOM,
RETENTION_RANGE, RETENTION_RANGE,
CUSTOM_INTERVAL,
} from '../../../helpers/constants'; } from '../../../helpers/constants';
import '../FormButton.css'; import '../FormButton.css';
@@ -26,7 +27,7 @@ const getIntervalTitle = (interval: number) => {
} }
}; };
type FormValues = { export type FormValues = {
enabled: boolean; enabled: boolean;
anonymize_client_ip: boolean; anonymize_client_ip: boolean;
interval: number; interval: number;
@@ -36,18 +37,18 @@ type FormValues = {
type Props = { type Props = {
initialValues: Partial<FormValues>; initialValues: Partial<FormValues>;
onSubmit: (values: FormValues) => void;
handleClear: () => void;
processing: boolean; processing: boolean;
processingClear: boolean; processingReset: boolean;
onSubmit: (values: FormValues) => void;
onReset: () => void;
} }
export const LogsConfigForm = ({ export const Form = ({
initialValues, initialValues,
onSubmit,
processing, processing,
processingClear, processingReset,
handleClear, onSubmit,
onReset,
}: Props) => { }: Props) => {
const { const {
register, register,
@@ -62,7 +63,7 @@ export const LogsConfigForm = ({
anonymize_client_ip: initialValues.anonymize_client_ip || false, anonymize_client_ip: initialValues.anonymize_client_ip || false,
interval: initialValues.interval || DAY, interval: initialValues.interval || DAY,
customInterval: initialValues.customInterval || null, customInterval: initialValues.customInterval || null,
ignored: initialValues.ignored ?? '', ignored: initialValues.ignored || '',
}, },
}); });
@@ -89,8 +90,6 @@ export const LogsConfigForm = ({
processing || processing ||
(intervalValue === RETENTION_CUSTOM && !customIntervalValue); (intervalValue === RETENTION_CUSTOM && !customIntervalValue);
console.log(intervalValue, RETENTION_CUSTOM, customIntervalValue);
return ( return (
<form onSubmit={handleSubmit(onSubmitForm)}> <form onSubmit={handleSubmit(onSubmitForm)}>
<div className="form__group form__group--settings"> <div className="form__group form__group--settings">
@@ -132,9 +131,9 @@ export const LogsConfigForm = ({
</label> </label>
</div> </div>
<label className="form__label"> <div className="form__label">
<Trans>query_log_retention</Trans> <Trans>query_log_retention</Trans>
</label> </div>
<div className="form__group form__group--settings"> <div className="form__group form__group--settings">
<div className="custom-controls-stacked"> <div className="custom-controls-stacked">
@@ -143,8 +142,8 @@ export const LogsConfigForm = ({
type="radio" type="radio"
className="custom-control-input" className="custom-control-input"
disabled={processing} disabled={processing}
value={RETENTION_CUSTOM}
checked={!QUERY_LOG_INTERVALS_DAYS.includes(intervalValue)} checked={!QUERY_LOG_INTERVALS_DAYS.includes(intervalValue)}
value={RETENTION_CUSTOM}
onChange={(e) => { onChange={(e) => {
setValue('interval', parseInt(e.target.value, 10)); setValue('interval', parseInt(e.target.value, 10));
}} }}
@@ -160,6 +159,7 @@ export const LogsConfigForm = ({
<input <input
type="number" type="number"
className="form-control" className="form-control"
name={CUSTOM_INTERVAL}
min={RETENTION_RANGE.MIN} min={RETENTION_RANGE.MIN}
max={RETENTION_RANGE.MAX} max={RETENTION_RANGE.MAX}
disabled={processing} disabled={processing}
@@ -220,8 +220,8 @@ export const LogsConfigForm = ({
<button <button
type="button" type="button"
className="btn btn-outline-secondary btn-standard form__button" className="btn btn-outline-secondary btn-standard form__button"
onClick={handleClear} onClick={onReset}
disabled={processingClear} disabled={processingReset}
> >
<Trans>query_log_clear</Trans> <Trans>query_log_clear</Trans>
</button> </button>

View File

@@ -3,7 +3,7 @@ import { withTranslation } from 'react-i18next';
import Card from '../../ui/Card'; import Card from '../../ui/Card';
import { LogsConfigForm } from './Form'; import { Form, FormValues } from './Form';
import { HOUR } from '../../../helpers/constants'; import { HOUR } from '../../../helpers/constants';
interface LogsConfigProps { interface LogsConfigProps {
@@ -20,7 +20,7 @@ interface LogsConfigProps {
} }
class LogsConfig extends Component<LogsConfigProps> { class LogsConfig extends Component<LogsConfigProps> {
handleFormSubmit = (values: any) => { handleFormSubmit = (values: FormValues) => {
const { t, interval: prevInterval } = this.props; const { t, interval: prevInterval } = this.props;
const { interval, customInterval, ...rest } = values; const { interval, customInterval, ...rest } = values;
@@ -65,7 +65,7 @@ class LogsConfig extends Component<LogsConfigProps> {
return ( return (
<Card title={t('query_log_configuration')} bodyType="card-body box-body--settings" id="logs-config"> <Card title={t('query_log_configuration')} bodyType="card-body box-body--settings" id="logs-config">
<div className="form"> <div className="form">
<LogsConfigForm <Form
initialValues={{ initialValues={{
enabled, enabled,
interval, interval,
@@ -73,10 +73,10 @@ class LogsConfig extends Component<LogsConfigProps> {
anonymize_client_ip, anonymize_client_ip,
ignored: ignored?.join('\n'), ignored: ignored?.join('\n'),
}} }}
onSubmit={this.handleFormSubmit}
processing={processing} processing={processing}
processingClear={processingClear} processingReset={processingClear}
handleClear={this.handleClear} onSubmit={this.handleFormSubmit}
onReset={this.handleClear}
/> />
</div> </div>
</Card> </Card>

View File

@@ -98,6 +98,10 @@
margin: 0 0 8px; margin: 0 0 8px;
} }
.form__label {
margin-bottom: 8px;
}
.form__label--bold { .form__label--bold {
font-weight: 700; font-weight: 700;
} }

View File

@@ -1,23 +1,12 @@
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
import { change, Field, formValueSelector, reduxForm } from 'redux-form'; import { Trans } from 'react-i18next';
import { Trans, withTranslation } from 'react-i18next'; import i18next from 'i18next';
import flow from 'lodash/flow';
import { connect } from 'react-redux';
import { useForm } from 'react-hook-form';
import { import {
renderRadioField,
toNumber,
CheckboxField,
renderTextareaField,
toFloatNumber,
renderInputField,
} from '../../../helpers/form';
import {
FORM_NAME,
STATS_INTERVALS_DAYS, STATS_INTERVALS_DAYS,
DAY, DAY,
RETENTION_CUSTOM, RETENTION_CUSTOM,
RETENTION_CUSTOM_INPUT,
CUSTOM_INTERVAL, CUSTOM_INTERVAL,
RETENTION_RANGE, RETENTION_RANGE,
} from '../../../helpers/constants'; } from '../../../helpers/constants';
@@ -25,66 +14,102 @@ import {
import { trimLinesAndRemoveEmpty } from '../../../helpers/helpers'; import { trimLinesAndRemoveEmpty } from '../../../helpers/helpers';
import '../FormButton.css'; import '../FormButton.css';
const getIntervalTitle = (intervalMs: any, t: any) => { const getIntervalTitle = (interval: any) => {
switch (intervalMs) { switch (interval) {
case RETENTION_CUSTOM: case RETENTION_CUSTOM:
return t('settings_custom'); return i18next.t('settings_custom');
case DAY: case DAY:
return t('interval_24_hour'); return i18next.t('interval_24_hour');
default: default:
return t('interval_days', { count: intervalMs / DAY }); return i18next.t('interval_days', { count: interval / DAY });
} }
}; };
interface FormProps { export type FormValues = {
handleSubmit: (...args: unknown[]) => string; enabled: boolean;
handleReset: (...args: unknown[]) => string; interval: number;
change: (...args: unknown[]) => unknown; customInterval?: number | null;
submitting: boolean; ignored: string;
invalid: boolean;
processing: boolean;
processingReset: boolean;
t: (...args: unknown[]) => string;
interval?: number;
customInterval?: number;
dispatch: (...args: unknown[]) => unknown;
} }
let Form = (props: FormProps) => { type Props = {
initialValues: Partial<FormValues>;
processing: boolean;
processingReset: boolean;
onSubmit: (values: FormValues) => void;
onReset: () => void;
}
export const Form = ({
initialValues,
processing,
processingReset,
onSubmit,
onReset,
}: Props) => {
const { const {
register,
handleSubmit, handleSubmit,
processing, watch,
submitting, setValue,
invalid, formState: { isSubmitting },
handleReset, } = useForm<FormValues>({
processingReset, mode: 'onChange',
t, defaultValues: {
interval, enabled: initialValues.enabled || false,
customInterval, interval: initialValues.interval || DAY,
dispatch, customInterval: initialValues.customInterval || null,
} = props; ignored: initialValues.ignored || '',
},
});
const intervalValue = watch('interval');
const customIntervalValue = watch('customInterval');
useEffect(() => { useEffect(() => {
if (STATS_INTERVALS_DAYS.includes(interval)) { if (STATS_INTERVALS_DAYS.includes(intervalValue)) {
dispatch(change(FORM_NAME.STATS_CONFIG, CUSTOM_INTERVAL, null)); setValue('customInterval', null);
} }
}, [interval]); }, [intervalValue]);
const onSubmitForm = (data: FormValues) => {
onSubmit(data);
};
const handleIgnoredBlur = (e: React.FocusEvent<HTMLTextAreaElement>) => {
const trimmed = trimLinesAndRemoveEmpty(e.target.value);
setValue('ignored', trimmed);
};
const disableSubmit =
isSubmitting ||
processing ||
(intervalValue === RETENTION_CUSTOM && !customIntervalValue);
return ( return (
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit(onSubmitForm)}>
<div className="form__group form__group--settings"> <div className="form__group form__group--settings">
<Field <label className="checkbox">
name="enabled" <span className="checkbox__marker" />
type="checkbox"
component={CheckboxField} <input
placeholder={t('statistics_enable')} type="checkbox"
disabled={processing} className="checkbox__input"
/> {...register('enabled')}
disabled={processing}
/>
<span className="checkbox__label">
<span className="checkbox__label-text checkbox__label-text--long">
<span className="checkbox__label-title">{i18next.t('statistics_enable')}</span>
</span>
</span>
</label>
</div> </div>
<label className="form__label form__label--with-desc"> <div className="form__label form__label--with-desc">
<Trans>statistics_retention</Trans> <Trans>statistics_retention</Trans>
</label> </div>
<div className="form__desc form__desc--top"> <div className="form__desc form__desc--top">
<Trans>statistics_retention_desc</Trans> <Trans>statistics_retention_desc</Trans>
@@ -92,21 +117,28 @@ let Form = (props: FormProps) => {
<div className="form__group form__group--settings mt-2"> <div className="form__group form__group--settings mt-2">
<div className="custom-controls-stacked"> <div className="custom-controls-stacked">
<Field <label className="custom-control custom-radio">
key={RETENTION_CUSTOM} <input
name="interval" type="radio"
type="radio" className="custom-control-input"
component={renderRadioField} disabled={processing}
value={STATS_INTERVALS_DAYS.includes(interval) ? RETENTION_CUSTOM : interval} checked={!STATS_INTERVALS_DAYS.includes(intervalValue)}
placeholder={getIntervalTitle(RETENTION_CUSTOM, t)} value={RETENTION_CUSTOM}
normalize={toFloatNumber} onChange={(e) => {
disabled={processing} setValue('interval', parseInt(e.target.value, 10));
/> }}
{!STATS_INTERVALS_DAYS.includes(interval) && ( />
<div className="form__group--input">
<div className="form__desc form__desc--top">{t('custom_retention_input')}</div>
<Field <span className="custom-control-label">{getIntervalTitle(RETENTION_CUSTOM)}</span>
</label>
{!STATS_INTERVALS_DAYS.includes(intervalValue) && (
<div className="form__group--input">
<div className="form__desc form__desc--top">
{i18next.t('custom_retention_input')}
</div>
{/* <Field
key={RETENTION_CUSTOM_INPUT} key={RETENTION_CUSTOM_INPUT}
name={CUSTOM_INTERVAL} name={CUSTOM_INTERVAL}
type="number" type="number"
@@ -116,34 +148,61 @@ let Form = (props: FormProps) => {
normalize={toFloatNumber} normalize={toFloatNumber}
min={RETENTION_RANGE.MIN} min={RETENTION_RANGE.MIN}
max={RETENTION_RANGE.MAX} max={RETENTION_RANGE.MAX}
/> */}
<input
name={CUSTOM_INTERVAL}
type="number"
className="form-control"
min={RETENTION_RANGE.MIN}
max={RETENTION_RANGE.MAX}
disabled={processing}
{...register('customInterval')}
onChange={(e) => {
setValue('customInterval', parseInt(e.target.value, 10));
}}
/> />
</div> </div>
)} )}
{STATS_INTERVALS_DAYS.map((interval) => ( {STATS_INTERVALS_DAYS.map((interval) => (
<Field // <Field
key={interval} // key={interval}
name="interval" // name="interval"
type="radio" // type="radio"
component={renderRadioField} // component={renderRadioField}
value={interval} // value={interval}
placeholder={getIntervalTitle(interval, t)} // placeholder={getIntervalTitle(interval, t)}
normalize={toNumber} // normalize={toNumber}
disabled={processing} // disabled={processing}
/> // />
<label key={interval} className="custom-control custom-radio">
<input
type="radio"
className="custom-control-input"
disabled={processing}
value={interval}
checked={intervalValue === interval}
onChange={(e) => {
setValue("interval", parseInt(e.target.value, 10));
}}
/>
<span className="custom-control-label">{getIntervalTitle(interval)}</span>
</label>
))} ))}
</div> </div>
</div> </div>
<label className="form__label form__label--with-desc"> <div className="form__label form__label--with-desc">
<Trans>ignore_domains_title</Trans> <Trans>ignore_domains_title</Trans>
</label> </div>
<div className="form__desc form__desc--top"> <div className="form__desc form__desc--top">
<Trans>ignore_domains_desc_stats</Trans> <Trans>ignore_domains_desc_stats</Trans>
</div> </div>
<div className="form__group form__group--settings"> <div className="form__group form__group--settings">
<Field {/* <Field
name="ignored" name="ignored"
type="textarea" type="textarea"
className="form-control form-control--textarea font-monospace text-input" className="form-control form-control--textarea font-monospace text-input"
@@ -151,6 +210,13 @@ let Form = (props: FormProps) => {
placeholder={t('ignore_domains')} placeholder={t('ignore_domains')}
disabled={processing} disabled={processing}
normalizeOnBlur={trimLinesAndRemoveEmpty} normalizeOnBlur={trimLinesAndRemoveEmpty}
/> */}
<textarea
className="form-control form-control--textarea font-monospace text-input"
placeholder={i18next.t('ignore_domains')}
disabled={processing}
{...register('ignored')}
onBlur={handleIgnoredBlur}
/> />
</div> </div>
@@ -158,19 +224,15 @@ let Form = (props: FormProps) => {
<button <button
type="submit" type="submit"
className="btn btn-success btn-standard btn-large" className="btn btn-success btn-standard btn-large"
disabled={ disabled={disableSubmit}
submitting || >
invalid ||
processing ||
(!STATS_INTERVALS_DAYS.includes(interval) && !customInterval)
}>
<Trans>save_btn</Trans> <Trans>save_btn</Trans>
</button> </button>
<button <button
type="button" type="button"
className="btn btn-outline-secondary btn-standard form__button" className="btn btn-outline-secondary btn-standard form__button"
onClick={() => handleReset()} onClick={onReset}
disabled={processingReset}> disabled={processingReset}>
<Trans>statistics_clear</Trans> <Trans>statistics_clear</Trans>
</button> </button>
@@ -178,16 +240,3 @@ let Form = (props: FormProps) => {
</form> </form>
); );
}; };
const selector = formValueSelector(FORM_NAME.STATS_CONFIG);
Form = connect((state) => {
const interval = selector(state, 'interval');
const customInterval = selector(state, CUSTOM_INTERVAL);
return {
interval,
customInterval,
};
})(Form);
export default flow([withTranslation(), reduxForm({ form: FORM_NAME.STATS_CONFIG })])(Form);

View File

@@ -3,7 +3,7 @@ import { withTranslation } from 'react-i18next';
import Card from '../../ui/Card'; import Card from '../../ui/Card';
import Form from './Form'; import { Form, FormValues } from './Form';
import { HOUR } from '../../../helpers/constants'; import { HOUR } from '../../../helpers/constants';
interface StatsConfigProps { interface StatsConfigProps {
@@ -19,7 +19,7 @@ interface StatsConfigProps {
} }
class StatsConfig extends Component<StatsConfigProps> { class StatsConfig extends Component<StatsConfigProps> {
handleFormSubmit = ({ enabled, interval, ignored, customInterval }: any) => { handleFormSubmit = ({ enabled, interval, ignored, customInterval }: FormValues) => {
const { t, interval: prevInterval } = this.props; const { t, interval: prevInterval } = this.props;
const newInterval = customInterval ? customInterval * HOUR : interval; const newInterval = customInterval ? customInterval * HOUR : interval;
@@ -49,17 +49,11 @@ class StatsConfig extends Component<StatsConfigProps> {
render() { render() {
const { const {
t, t,
interval, interval,
customInterval, customInterval,
processing, processing,
processingReset, processingReset,
ignored, ignored,
enabled, enabled,
} = this.props; } = this.props;
@@ -73,10 +67,10 @@ class StatsConfig extends Component<StatsConfigProps> {
enabled, enabled,
ignored: ignored.join('\n'), ignored: ignored.join('\n'),
}} }}
onSubmit={this.handleFormSubmit}
processing={processing} processing={processing}
processingReset={processingReset} processingReset={processingReset}
handleReset={this.handleReset} onSubmit={this.handleFormSubmit}
onReset={this.handleReset}
/> />
</div> </div>
</Card> </Card>