All files / src/app/pages/checkout-address/checkout-address-anonymous checkout-address-anonymous.component.ts

86.48% Statements 32/37
68.42% Branches 13/19
87.5% Functions 7/8
86.48% Lines 32/37

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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 1262x 2x   2x 2x 2x 2x                                   2x       10x       10x     10x 10x   10x         1x                 1x                 1x   1x   1x         1x       1x             3x 2x 2x 2x       1x           1x       1x 1x           1x       1x       1x         9x       18x      
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core';
import { FormGroupDirective, UntypedFormGroup } from '@angular/forms';
 
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { Basket } from 'ish-core/models/basket/basket.model';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
 
/**
 * The Checkout Address Anonymous Component renders the initial checkout address page of an anonymous user. On this page the user can either login or checkout as guest by entering an invoice and (optionally) shipping address.
 * See also {@link CheckoutAddressPageContainerComponent}
 *
 * @example
 * <ish-checkout-address-anonymous
 *  [basket]="basket$ | async"
 *  [error]="(basketError$ | async) || (addressesError$ | async)"
 *  (createBasketAddress)="createBasketAddress($event)"
 * ></ish-checkout-address-anonymous>
 */
@Component({
  selector: 'ish-checkout-address-anonymous',
  templateUrl: './checkout-address-anonymous.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CheckoutAddressAnonymousComponent implements OnChanges {
  @Input({ required: true }) basket: Basket;
  @Input() error: HttpError;
 
  @Output() nextStep = new EventEmitter<void>();
 
  @ViewChild('addressForm') addressForm: FormGroupDirective;
 
  form = new UntypedFormGroup({});
 
  // visible-for-testing
  submitted = false;
  isAddressFormCollapsed = true;
 
  constructor(private checkoutFacade: CheckoutFacade) {}
  /**
    jumps to nextPage as soon as basket invoice and shipping addresses are set.
   */
  ngOnChanges() {
    Iif (this.isNextStepAvailable()) {
      this.nextStep.emit();
    }
  }
 
  /**
   * checks, if all data are available to jump to the next checkout step
   */
  private isNextStepAvailable() {
    return this.basket?.invoiceToAddress && this.basket.commonShipToAddress;
  }
 
  showAddressForm() {
    this.isAddressFormCollapsed = false;
    // do not close address form immediately to show possible server errors
  }
 
  cancelAddressForm() {
    this.isAddressFormCollapsed = true;
 
    this.addressForm.resetForm();
 
    this.addressForm.control.get('additionalAddressAttributes').setValue({
      taxationID: '',
      email: '',
    });
 
    this.addressForm.control.get('shipOptions').setValue({
      shipOption: 'shipToInvoiceAddress',
    });
 
    this.submitted = false;
  }
 
  /**
   * submits address form and leads to next checkout page (checkout shipping)
   */
  submitAddressForm() {
    if (this.form.invalid) {
      this.submitted = true;
      markAsDirtyRecursive(this.form);
      return;
    }
 
    // submit address form
    const invoiceAddress = {
      ...this.form.get('invoiceAddress').value.address,
      email: this.form.get('additionalAddressAttributes').value.email,
    };
 
    const shippingAddress =
      this.form.get('shipOptions').value.shipOption === 'shipToInvoiceAddress'
        ? undefined
        : this.form.get('shippingAddress').value.address;
 
    if (this.form.get('additionalAddressAttributes').get('taxationID')) {
      Iif (this.form.get('additionalAddressAttributes').value.taxationID) {
        this.checkoutFacade.setBasketCustomAttribute({
          name: 'taxationID',
          value: this.form.get('additionalAddressAttributes').value.taxationID,
        });
      } else {
        this.checkoutFacade.deleteBasketCustomAttribute('taxationID');
      }
    }
 
    Iif (shippingAddress) {
      this.checkoutFacade.createBasketAddress(invoiceAddress, 'invoice');
      this.checkoutFacade.createBasketAddress(shippingAddress, 'shipping');
    } else {
      this.checkoutFacade.createBasketAddress(invoiceAddress, 'any');
    }
  }
 
  get nextDisabled() {
    return this.form.invalid && this.submitted;
  }
 
  get isShippingAddressFormExpanded() {
    return this.form && this.form.get('shipOptions').value.shipOption === 'shipToDifferentAddress';
  }
}