Browse Source

Fixed dashboardId being corrupted and dirty-check bypassed in alarm rule form

pull/15868/head
Maksym Tsymbarov 3 months ago
parent
commit
b79d3286e4
  1. 27
      ui-ngx/src/app/modules/home/components/alarm-rules/cf-alarm-rule.component.ts

27
ui-ngx/src/app/modules/home/components/alarm-rules/cf-alarm-rule.component.ts

@ -76,12 +76,11 @@ export class CfAlarmRuleComponent implements ControlValueAccessor, OnInit, Valid
testScript: (expression: string) => Observable<string>; testScript: (expression: string) => Observable<string>;
private modelValue: AlarmRule; private modelValue: AlarmRule;
private propagatedValue: AlarmRule;
alarmRuleFormGroup = this.fb.group({ alarmRuleFormGroup = this.fb.group({
condition: this.fb.control<AlarmRuleCondition | null>(null, Validators.required), condition: this.fb.control<AlarmRuleCondition | null>(null, Validators.required),
alarmDetails: [null], alarmDetails: this.fb.control<string | null>(null),
dashboardId: [null] dashboardId: this.fb.control<string | null>(null)
}); });
private propagateChange = (v: any) => { }; private propagateChange = (v: any) => { };
@ -106,8 +105,8 @@ export class CfAlarmRuleComponent implements ControlValueAccessor, OnInit, Valid
ngOnInit() { ngOnInit() {
this.alarmRuleFormGroup.valueChanges.pipe( this.alarmRuleFormGroup.valueChanges.pipe(
takeUntilDestroyed(this.destroyRef) takeUntilDestroyed(this.destroyRef)
).subscribe(() => { ).subscribe((value) => {
this.updateModel(); this.updateModel(value);
}); });
this.alarmRuleFormGroup.statusChanges.pipe( this.alarmRuleFormGroup.statusChanges.pipe(
takeUntilDestroyed(this.destroyRef) takeUntilDestroyed(this.destroyRef)
@ -126,13 +125,12 @@ export class CfAlarmRuleComponent implements ControlValueAccessor, OnInit, Valid
} }
writeValue(value: AlarmRule): void { writeValue(value: AlarmRule): void {
this.modelValue = value; const model = value ? {
const model = this.modelValue ? { ...value,
...this.modelValue, dashboardId: value.dashboardId?.id
dashboardId: this.modelValue.dashboardId?.id
} : null; } : null;
this.alarmRuleFormGroup.patchValue(model, {emitEvent: false}); this.alarmRuleFormGroup.patchValue(model, {emitEvent: false});
this.propagatedValue = this.toModel(this.alarmRuleFormGroup.value); this.modelValue = model ? this.toModel(model) : null;
} }
public openEditDetailsDialog($event: Event) { public openEditDetailsDialog($event: Event) {
@ -162,17 +160,16 @@ export class CfAlarmRuleComponent implements ControlValueAccessor, OnInit, Valid
}; };
} }
private updateModel() { private updateModel(value: typeof this.alarmRuleFormGroup.value): void {
const modelValue = this.toModel(this.alarmRuleFormGroup.value); const modelValue = this.toModel(value);
if (isEqual(modelValue, this.propagatedValue)) { if (isEqual(modelValue, this.modelValue)) {
return; return;
} }
this.modelValue = modelValue; this.modelValue = modelValue;
this.propagatedValue = modelValue;
this.propagateChange(modelValue); this.propagateChange(modelValue);
} }
private toModel(value: any): AlarmRule { private toModel(value: typeof this.alarmRuleFormGroup.value): AlarmRule {
return {...value, dashboardId: value.dashboardId ? new DashboardId(value.dashboardId) : null} as AlarmRule; return {...value, dashboardId: value.dashboardId ? new DashboardId(value.dashboardId) : null} as AlarmRule;
} }
} }

Loading…
Cancel
Save