All files / src/app/shared/components/basket/basket-order-reference basket-order-reference.component.ts

91.66% Statements 22/24
87.5% Branches 14/16
85.71% Functions 6/7
91.3% Lines 21/23

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931x                   1x     1x 1x 1x             1x     5x 5x     5x   5x     5x                                                     1x 1x 1x         1x         1x 1x               7x       2x 1x   1x      
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  Input,
  OnChanges,
  OnInit,
  SimpleChange,
  SimpleChanges,
} from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
 
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { Basket } from 'ish-core/models/basket/basket.model';
import { SpecialValidators } from 'ish-shared/forms/validators/special-validators';
 
@Component({
  selector: 'ish-basket-order-reference',
  templateUrl: './basket-order-reference.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BasketOrderReferenceComponent implements OnInit, OnChanges {
  @Input({ required: true }) basket: Basket;
 
  form = new FormGroup({});
  model: { orderReferenceId: string } = { orderReferenceId: '' };
  fields: FormlyFieldConfig[];
 
  showSuccessMessage = false;
 
  constructor(private checkoutFacade: CheckoutFacade, private cd: ChangeDetectorRef) {}
 
  ngOnInit() {
    this.fields = [
      {
        key: 'orderReferenceId',
        type: 'ish-text-input-field',
        props: {
          postWrappers: [{ wrapper: 'description', index: -1 }],
          label: 'checkout.orderReferenceId.label',
          maxLength: 35,
          customDescription: {
            key: 'checkout.orderReferenceId.note',
          },
          labelClass: 'col-md-6',
          fieldClass: 'col-md-6',
        },
        validators: {
          validation: [SpecialValidators.noSpecialChars],
        },
        validation: {
          messages: {
            noSpecialChars: 'account.name.error.forbidden.chars',
          },
        },
      },
    ];
  }
 
  ngOnChanges(changes: SimpleChanges) {
    if (this.basket) {
      this.successMessage(changes.basket);
      this.model = { ...this.model, orderReferenceId: this.basket.externalOrderReference };
    }
  }
 
  private successMessage(basketChange: SimpleChange) {
    if (
      (basketChange?.previousValue as Basket)?.externalOrderReference !==
        (basketChange?.currentValue as Basket)?.externalOrderReference &&
      !basketChange?.firstChange
    ) {
      this.showSuccessMessage = true;
      setTimeout(() => {
        this.showSuccessMessage = false;
        this.cd.markForCheck();
      }, 5000);
    }
  }
 
  get disabled() {
    return this.form.invalid || (!this.basket?.externalOrderReference && !this.form.get('orderReferenceId').value);
  }
 
  submitForm() {
    if (this.disabled) {
      return;
    }
    this.checkoutFacade.updateBasketExternalOrderReference(this.form.get('orderReferenceId').value);
  }
}