|
1 | 1 | /** Angular Imports */ |
2 | 2 | import { Component, OnInit } from '@angular/core'; |
3 | 3 | import { ActivatedRoute, RouterLink } from '@angular/router'; |
4 | | -import { UntypedFormControl, UntypedFormGroup, Validators, ReactiveFormsModule } from '@angular/forms'; |
| 4 | +import { |
| 5 | + AbstractControl, |
| 6 | + UntypedFormControl, |
| 7 | + UntypedFormGroup, |
| 8 | + ValidatorFn, |
| 9 | + Validators, |
| 10 | + ReactiveFormsModule |
| 11 | +} from '@angular/forms'; |
5 | 12 |
|
6 | 13 | /** Custom Services */ |
7 | 14 | import { ReportsService } from '../reports.service'; |
@@ -176,6 +183,7 @@ export class RunReportComponent implements OnInit { |
176 | 183 | } |
177 | 184 | this.decimalChoice.patchValue('0'); |
178 | 185 | this.setChildControls(); |
| 186 | + this.addDateRangeValidator(); |
179 | 187 | } |
180 | 188 |
|
181 | 189 | /** |
@@ -206,6 +214,62 @@ export class RunReportComponent implements OnInit { |
206 | 214 | }); |
207 | 215 | } |
208 | 216 |
|
| 217 | + addDateRangeValidator(): void { |
| 218 | + const dateParams = this.paramData.filter((param: ReportParameter) => param.displayType === 'date'); |
| 219 | + const startParam = dateParams.find((param: ReportParameter) => this.isStartDateParam(param)); |
| 220 | + const endParam = dateParams.find((param: ReportParameter) => this.isEndDateParam(param)); |
| 221 | + |
| 222 | + if (!startParam || !endParam) { |
| 223 | + return; |
| 224 | + } |
| 225 | + |
| 226 | + const startControl = this.reportForm.get(startParam.name); |
| 227 | + const endControl = this.reportForm.get(endParam.name); |
| 228 | + |
| 229 | + if (!startControl || !endControl) { |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + endControl.addValidators(this.endDateAfterStartValidator(startParam.name)); |
| 234 | + endControl.updateValueAndValidity({ emitEvent: false }); |
| 235 | + startControl.valueChanges.subscribe(() => endControl.updateValueAndValidity({ emitEvent: false })); |
| 236 | + } |
| 237 | + |
| 238 | + endDateAfterStartValidator(startControlName: string): ValidatorFn { |
| 239 | + return (control: AbstractControl) => { |
| 240 | + const startControl = control.parent?.get(startControlName); |
| 241 | + const startValue = startControl?.value; |
| 242 | + const endValue = control.value; |
| 243 | + |
| 244 | + if (!startValue || !endValue) { |
| 245 | + return null; |
| 246 | + } |
| 247 | + |
| 248 | + const startDate = new Date(startValue); |
| 249 | + const endDate = new Date(endValue); |
| 250 | + |
| 251 | + if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) { |
| 252 | + return null; |
| 253 | + } |
| 254 | + |
| 255 | + if (endDate < startDate) { |
| 256 | + return { endBeforeStart: true }; |
| 257 | + } |
| 258 | + |
| 259 | + return null; |
| 260 | + }; |
| 261 | + } |
| 262 | + |
| 263 | + isStartDateParam(param: ReportParameter): boolean { |
| 264 | + const identifier = `${param.name}${param.variable}${param.label}`.toLowerCase(); |
| 265 | + return identifier.includes('start') || identifier.includes('from'); |
| 266 | + } |
| 267 | + |
| 268 | + isEndDateParam(param: ReportParameter): boolean { |
| 269 | + const identifier = `${param.name}${param.variable}${param.label}`.toLowerCase(); |
| 270 | + return identifier.includes('end') || identifier.includes('to'); |
| 271 | + } |
| 272 | + |
209 | 273 | /** |
210 | 274 | * Subscribes to changes in parent parameters value, builds child parameter vis-a-vis parent's value. |
211 | 275 | */ |
|
0 commit comments