Timewindow: datapoints limit validation fix

This commit is contained in:
Chantsova Ekaterina 2024-09-12 11:46:06 +03:00
parent e9475e5b06
commit 56d7ab3bb6
3 changed files with 35 additions and 31 deletions

View File

@ -15,9 +15,17 @@
/// ///
import { Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core'; import { Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core';
import { ControlValueAccessor, FormBuilder, NG_VALUE_ACCESSOR, UntypedFormGroup, Validators } from '@angular/forms'; import {
ControlValueAccessor,
FormBuilder,
FormGroup,
NG_VALIDATORS,
NG_VALUE_ACCESSOR,
ValidationErrors,
Validator,
Validators
} from '@angular/forms';
import { coerceBooleanProperty } from '@angular/cdk/coercion'; import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { getTimezoneInfo } from '@shared/models/time/time.models';
import { TimeService } from '@core/services/time.service'; import { TimeService } from '@core/services/time.service';
import { takeUntil } from 'rxjs/operators'; import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs'; import { Subject } from 'rxjs';
@ -26,15 +34,22 @@ import { Subject } from 'rxjs';
selector: 'tb-datapoints-limit', selector: 'tb-datapoints-limit',
templateUrl: './datapoints-limit.component.html', templateUrl: './datapoints-limit.component.html',
styleUrls: ['./datapoints-limit.component.scss'], styleUrls: ['./datapoints-limit.component.scss'],
providers: [{ providers: [
{
provide: NG_VALUE_ACCESSOR, provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DatapointsLimitComponent), useExisting: forwardRef(() => DatapointsLimitComponent),
multi: true multi: true
}] },
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => DatapointsLimitComponent),
multi: true
}
]
}) })
export class DatapointsLimitComponent implements ControlValueAccessor, OnInit, OnDestroy { export class DatapointsLimitComponent implements ControlValueAccessor, Validator, OnInit, OnDestroy {
datapointsLimitFormGroup: UntypedFormGroup; datapointsLimitFormGroup: FormGroup;
modelValue: number | null; modelValue: number | null;
@ -71,7 +86,7 @@ export class DatapointsLimitComponent implements ControlValueAccessor, OnInit, O
ngOnInit() { ngOnInit() {
this.datapointsLimitFormGroup = this.fb.group({ this.datapointsLimitFormGroup = this.fb.group({
limit: [null] limit: [null, [Validators.min(this.minDatapointsLimit()), Validators.max(this.maxDatapointsLimit())]]
}); });
this.datapointsLimitFormGroup.get('limit').valueChanges.pipe( this.datapointsLimitFormGroup.get('limit').valueChanges.pipe(
takeUntil(this.destroy$) takeUntil(this.destroy$)
@ -82,10 +97,11 @@ export class DatapointsLimitComponent implements ControlValueAccessor, OnInit, O
updateValidators() { updateValidators() {
if (this.datapointsLimitFormGroup) { if (this.datapointsLimitFormGroup) {
this.datapointsLimitFormGroup.get('limit').setValidators(this.required if (this.required) {
? [Validators.required, Validators.min(this.minDatapointsLimit()), this.datapointsLimitFormGroup.get('limit').addValidators(Validators.required);
Validators.max(this.maxDatapointsLimit())] } else {
: []); this.datapointsLimitFormGroup.get('limit').removeValidators(Validators.required);
}
this.datapointsLimitFormGroup.get('limit').updateValueAndValidity(); this.datapointsLimitFormGroup.get('limit').updateValueAndValidity();
} }
} }
@ -122,6 +138,12 @@ export class DatapointsLimitComponent implements ControlValueAccessor, OnInit, O
} }
} }
validate(): ValidationErrors {
return this.datapointsLimitFormGroup.get('limit').valid ? null : {
datapointsLimitFormGroup: false,
};
}
minDatapointsLimit() { minDatapointsLimit() {
return this.timeService.getMinDatapointsLimit(); return this.timeService.getMinDatapointsLimit();
} }

View File

@ -296,8 +296,7 @@ export class TimewindowConfigDialogComponent extends PageComponent implements On
if (aggType !== AggregationType.NONE) { if (aggType !== AggregationType.NONE) {
this.timewindowForm.get('aggregation.limit').clearValidators(); this.timewindowForm.get('aggregation.limit').clearValidators();
} else { } else {
this.timewindowForm.get('aggregation.limit').setValidators([Validators.min(this.minDatapointsLimit()), this.timewindowForm.get('aggregation.limit').setValidators([Validators.required]);
Validators.max(this.maxDatapointsLimit())]);
} }
this.timewindowForm.get('aggregation.limit').updateValueAndValidity({emitEvent: false}); this.timewindowForm.get('aggregation.limit').updateValueAndValidity({emitEvent: false});
} }
@ -351,14 +350,6 @@ export class TimewindowConfigDialogComponent extends PageComponent implements On
this.dialogRef.close(); this.dialogRef.close();
} }
minDatapointsLimit() {
return this.timeService.getMinDatapointsLimit();
}
maxDatapointsLimit() {
return this.timeService.getMaxDatapointsLimit();
}
minRealtimeAggInterval() { minRealtimeAggInterval() {
return this.timeService.minIntervalLimit(this.currentRealtimeTimewindow()); return this.timeService.minIntervalLimit(this.currentRealtimeTimewindow());
} }

View File

@ -307,8 +307,7 @@ export class TimewindowPanelComponent extends PageComponent implements OnInit, O
if (aggType !== AggregationType.NONE) { if (aggType !== AggregationType.NONE) {
this.timewindowForm.get('aggregation.limit').clearValidators(); this.timewindowForm.get('aggregation.limit').clearValidators();
} else { } else {
this.timewindowForm.get('aggregation.limit').setValidators([Validators.min(this.minDatapointsLimit()), this.timewindowForm.get('aggregation.limit').setValidators([Validators.required]);
Validators.max(this.maxDatapointsLimit())]);
} }
this.timewindowForm.get('aggregation.limit').updateValueAndValidity({emitEvent: false}); this.timewindowForm.get('aggregation.limit').updateValueAndValidity({emitEvent: false});
} }
@ -456,14 +455,6 @@ export class TimewindowPanelComponent extends PageComponent implements OnInit, O
this.overlayRef.dispose(); this.overlayRef.dispose();
} }
minDatapointsLimit() {
return this.timeService.getMinDatapointsLimit();
}
maxDatapointsLimit() {
return this.timeService.getMaxDatapointsLimit();
}
minRealtimeAggInterval() { minRealtimeAggInterval() {
return this.timeService.minIntervalLimit(this.currentRealtimeTimewindow()); return this.timeService.minIntervalLimit(this.currentRealtimeTimewindow());
} }