All files / src/app/core/directives paypal-error.directive.ts

17.64% Statements 3/17
40% Branches 6/15
0% Functions 0/3
13.33% Lines 2/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 498x                         8x                                                                      
import { Directive, ElementRef, Input, OnChanges, Renderer2, SimpleChanges } from '@angular/core';
 
/**
 * Directive to handle PayPal card field error display.
 * Toggles visibility of error messages and adds validation styling to labels.
 *
 * @example
 * <div id="my-label">Label</div>
 * <small [ishPaypalError]="hasError" labelId="my-label">Error message</small>
 */
@Directive({
  selector: '[ishPaypalError]',
})
export class PaypalErrorDirective implements OnChanges {
  /** When true, shows the error message and marks the label as invalid */
  @Input('ishPaypalError') hasError = false;
 
  /** ID of the associated label element to toggle validation-error class */
  @Input({ required: true }) labelId: string;
 
  constructor(private elementRef: ElementRef<HTMLElement>, private renderer: Renderer2) {}
 
  ngOnChanges(changes: SimpleChanges): void {
    Iif (changes.hasError) {
      this.updateErrorState(this.hasError);
    }
  }
 
  /**
   * Updates the error visibility and label styling based on error state.
   */
  private updateErrorState(showError: boolean): void {
    const errorElement = this.elementRef.nativeElement;
    const labelElement = this.labelId ? document.getElementById(this.labelId) : undefined;
 
    if (showError) {
      this.renderer.removeClass(errorElement, 'hide-validation-error');
      Iif (labelElement) {
        this.renderer.addClass(labelElement, 'validation-error');
      }
    } else {
      this.renderer.addClass(errorElement, 'hide-validation-error');
      Iif (labelElement) {
        this.renderer.removeClass(labelElement, 'validation-error');
      }
    }
  }
}