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

83.87% Statements 26/31
64.7% Branches 11/17
87.5% Functions 7/8
83.87% Lines 26/31

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 1182x 2x   2x 2x 2x                                   2x       11x       11x     11x   11x         1x                 1x       11x                 1x   1x   1x         1x                 2x   1x           1x       1x 1x           1x       1x       1x           20x      
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';
 
/**
 * 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
  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;
  }
 
  isRecurringOrder() {
    return this.basket?.recurrence;
  }
 
  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',
    });
  }
 
  /**
   * submits address form and leads to next checkout page (checkout shipping)
   */
  submitAddressForm() {
    if (this.form.valid) {
      // 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 isShippingAddressFormExpanded() {
    return this.form && this.form.get('shipOptions').value.shipOption === 'shipToDifferentAddress';
  }
}