From 6d236738a0f15a6e264e7bf1ae8de82304d2b669 Mon Sep 17 00:00:00 2001 From: Sam Warren Date: Wed, 11 Dec 2024 12:56:51 -0800 Subject: [PATCH] DEVOPS-175: Fix validation logic for support end dates / times --- .../support-details.component.ts | 60 ++++++++----------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/responders/src/UI/embc-responder/src/app/feature-components/wizard/support-components/support-details/support-details.component.ts b/responders/src/UI/embc-responder/src/app/feature-components/wizard/support-components/support-details/support-details.component.ts index 0df0273859..c1b4178384 100644 --- a/responders/src/UI/embc-responder/src/app/feature-components/wizard/support-components/support-details/support-details.component.ts +++ b/responders/src/UI/embc-responder/src/app/feature-components/wizard/support-components/support-details/support-details.component.ts @@ -151,62 +151,52 @@ export class SupportDetailsComponent implements OnInit, OnDestroy { compareTaskDateTimeValidator({ controlType, other }: { controlType: 'date' | 'time'; other: string }): ValidatorFn { return (control: AbstractControl): ValidationErrors | null => { let isValid = false; - const error = { invalidTaskDateTime: true }; const otherControl = this.supportDetailsForm?.get(other); - // no need to validate if one of the controls does not have a value - if ( - control?.value === null || - control?.value === '' || - control?.value === undefined || - otherControl?.value === null || - otherControl?.value === '' || - otherControl?.value === undefined - ) { + const error = { invalidTaskDateTime: true }; + + if (!control?.value || !otherControl?.value) { isValid = true; } else { let controlDate; if (controlType === 'date') { controlDate = this.dateConversionService.createDateTimeString(control.value, otherControl.value); - } else if (controlType === 'time') { + } else { controlDate = this.dateConversionService.createDateTimeString(otherControl.value, control.value); } if (this.evacueeSessionService?.evacFile?.task?.from && this.evacueeSessionService?.evacFile?.task?.to) { const from = moment(this.evacueeSessionService?.evacFile?.task?.from); const to = moment(this.evacueeSessionService?.evacFile?.task?.to); - isValid = moment(controlDate).isBetween(from, to, 'm', '[]'); - } else isValid = true; + } else { + isValid = true; + } } + if (!isValid) { - otherControl?.setErrors(error); - control?.setErrors(error); + control.setErrors({ ...control.errors, ...error }); + otherControl.setErrors({ ...otherControl.errors, ...error }); } else { - if (control.errors) { - const errors = { ...control.errors }; // Copy the errors object - delete errors['invalidTaskDateTime']; // Remove the specific error - - if (Object.keys(errors).length === 0) { - control.setErrors(null); // No errors left, set to null - } else { - control.setErrors(errors); // Set the modified errors object - } - if (otherControl.errors) { - const errors = { ...otherControl.errors }; // Copy the errors object - delete errors['invalidTaskDateTime']; // Remove the specific error - - if (Object.keys(errors).length === 0) { - otherControl.setErrors(null); // No errors left, set to null - } else { - otherControl.setErrors(errors); // Set the modified errors object - } - } - } + this.removeError(control, 'invalidTaskDateTime'); + this.removeError(otherControl, 'invalidTaskDateTime'); } + return null; }; } + private removeError(control: AbstractControl, errorKey: string): void { + if (control && control.errors && control.errors[errorKey]) { + const errors = { ...control.errors }; + delete errors[errorKey]; + if (Object.keys(errors).length === 0) { + control.setErrors(null); + } else { + control.setErrors(errors); + } + } + } + validDateFilter = (d: Date | null): boolean => { const date = d || new Date(); return moment(date).isBetween(