]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/validators/dates_in_order_validator.directive.ts
LP1850473: manual and automated eslint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / validators / dates_in_order_validator.directive.ts
1 /* eslint-disable no-unused-expressions */
2 import { Directive, Input } from '@angular/core';
3 import { AbstractControl, NG_VALIDATORS, ValidationErrors, Validator, ValidatorFn } from '@angular/forms';
4 import * as moment from 'moment';
5
6 export function datesInOrderValidator(fieldNames: string[]): ValidatorFn {
7     return (control: AbstractControl): {[key: string]: any} | null => {
8         if (fieldsAreInOrder(fieldNames, control)) {return null;}
9         return {datesOutOfOrder: 'Dates should be in order'};
10     };
11 }
12
13 function fieldsAreInOrder(fieldNames: string[], control: AbstractControl): boolean {
14     if (fieldNames.length === 0) {return true;}
15     return fieldNames.every((field, index) => {
16         // No need to compare field[0] to the field before it
17         if (index === 0) {return true;}
18
19         const previousValue = moment(control.get(fieldNames[index - 1])?.value);
20         const currentValue = moment(control.get(field)?.value);
21
22         // If either field is invalid, return true -- there should be other
23         // validation that can catch that
24         if (!previousValue.isValid() || !currentValue.isValid()) {return true;}
25
26         // Check each field against its predecessor
27         return previousValue.isSameOrBefore(currentValue);
28     });
29 }
30
31 @Directive({
32     selector: '[egDateFieldOrderList]',
33     providers: [{ provide: NG_VALIDATORS, useExisting: DatesInOrderValidatorDirective, multi: true }]
34 })
35 export class DatesInOrderValidatorDirective implements Validator {
36     @Input('egDateFieldOrderList') dateFieldOrderList = '';
37     validate(control: AbstractControl): ValidationErrors | null {
38         if (this.dateFieldOrderList?.length > 0) {
39             return datesInOrderValidator(this.dateFieldOrderList.split(','))(control);
40         } else {
41         // Don't run validations if we have no fields to examine
42             return () => {null;};
43         }
44     }
45 }