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

100% Statements 21/21
88.88% Branches 8/9
100% Functions 4/4
100% Lines 21/21

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 732x 2x 2x     2x             2x     2x 2x 2x 2x       2x   2x     3x     2x     2x 2x     2x                                       2x 2x 2x     2x       3x            
import { ChangeDetectionStrategy, Component, DestroyRef, Input, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
 
import { FeatureToggleService } from 'ish-core/feature-toggle.module';
 
@Component({
  selector: 'ish-checkout-address-anonymous-form',
  templateUrl: './checkout-address-anonymous-form.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CheckoutAddressAnonymousFormComponent implements OnInit {
  @Input({ required: true }) parentForm: FormGroup;
 
  invoiceAddressForm = new FormGroup({});
  shippingAddressForm = new FormGroup({});
  attributesForm: FormGroup = new FormGroup({});
  shipOptionForm: FormGroup = new FormGroup({});
 
  shipOptionFields: FormlyFieldConfig[];
 
  isBusinessCustomer = false;
 
  private destroyRef = inject(DestroyRef);
 
  get isShippingAddressFormExpanded() {
    return this.shipOptionForm && this.shipOptionForm.get('shipOption').value === 'shipToDifferentAddress';
  }
 
  constructor(private featureToggleService: FeatureToggleService) {}
 
  ngOnInit() {
    if (this.featureToggleService.enabled('businessCustomerRegistration')) {
      this.isBusinessCustomer = true;
    }
 
    this.shipOptionFields = [
      {
        type: 'ish-radio-field',
        key: 'shipOption',
        defaultValue: 'shipToInvoiceAddress',
        props: {
          label: 'checkout.addresses.shipping_address.option1.text',
          value: 'shipToInvoiceAddress',
        },
      },
      {
        type: 'ish-radio-field',
        key: 'shipOption',
        defaultValue: 'shipToInvoiceAddress',
        props: {
          label: 'checkout.addresses.shipping_address.option2.text',
          value: 'shipToDifferentAddress',
        },
      },
    ];
    this.parentForm.setControl('invoiceAddress', this.invoiceAddressForm);
    this.parentForm.setControl('additionalAddressAttributes', this.attributesForm);
    this.parentForm.setControl('shipOptions', this.shipOptionForm);
 
    // add / remove shipping form if shipTo address option changes
    this.parentForm
      .get('shipOptions')
      .valueChanges.pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(options => {
        options.shipOption === 'shipToInvoiceAddress'
          ? this.parentForm.removeControl('shippingAddress')
          : this.parentForm.setControl('shippingAddress', this.shippingAddressForm);
      });
  }
}