/** * @Component Name: PPUAddressComponent * @Module: Checkout * @Module Functionality : To display the customer information * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, OnInit, Input, OnChanges, OnDestroy } from '@angular/core'; import { FormBuilder, Validators } from '@angular/forms'; import Utils from '../../../../shared/utils'; import CartUtils from '../../../../shared/cart.utils'; import { STATES } from '../../../../shared/form.constants'; import * as Constants from '../../../../shared/constants'; import { IAddress } from '../../../../shared/modal/common'; import { IPackage } from '../../../../shared/modal/common'; import { SharedService } from '../../../../shared/shared.service'; import { CustomerService } from '../../../../services/customer.service'; import { CheckoutService } from '../../../../services/checkout.service'; import CheckoutUtils from '../../../../shared/checkout.utils'; @Component( { selector: 'ppu-address-cmp', moduleId: module.id, templateUrl: 'ppu-address.component.html' }) export class PPUAddressComponent implements OnInit, OnChanges, OnDestroy { private COMPONENT_NAME: string = 'PPUAddressComponent'; stateItems: any[]; addressModel: any = {state: ''}; @Input() billingAddressId: number; @Input() package: IPackage; storeId: number; // update storeid customerInfo: any; karmaTestResult: any = {}; notSameAddressId: boolean; constructor(private formBuilder: FormBuilder, public sharedService: SharedService, public customerService: CustomerService, public checkoutService: CheckoutService) { } /** * This function is used to initialize the directive/component after Angular * first displays the data-bound properties.It is invoked only once when the * directive is instantiated.Here it is initializing all the states, payment methods and card types * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; console.log('[start]-PPUAddressComponent.ngOnInit()'); this.stateItems = STATES.filter(stateItem => stateItem); // Start-Switch Channel-Store id update this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); // getting the customer info updates this.customerInfo = this.checkoutService.getCustomerInfo(); console.log('[end]-PPUAddressComponent.ngOnInit()'); this.notSameAddressId = (CheckoutUtils.getDeviceNumberService(this.billingAddressId, this.package, 'ppuAddress', 'addressId')); this.addressModel.sameAsBilling = this.notSameAddressId; this.addressModel.isValidPPU = !this.addressModel.sameAsBilling; const add = (CheckoutUtils.getDeviceServiceCustInfo(this.customerInfo, this.package, 'ppuAddress')); if (this.package && this.package.ppuAddress && !add.isNotSame) { this.addressModel = Object.assign( {}, this.addressModel, this.package.ppuAddress); } else { if (this.notSameAddressId) { if (add.isNotSame) { this.addressModel.sameAsBilling = !add.isNotSame; this.addressModel.isValidPPU = add.isValidPPU; this.addressModel = Object.assign( {}, this.addressModel, add.address); } } } } /** * This function is called whenever one or more data-bound input properties change * @returns void */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; console.log('customer data from parent:'); this.stateItems = STATES.filter(stateItem => stateItem); this.addressModel.sameAsBilling = true; if (this.package.ppuAddress) { this.addressModel.sameAsBilling = false; this.addressModel = Object.assign( {}, this.package.ppuAddress); if (this.billingAddressId && this.billingAddressId === this.package.ppuAddress.addressId) { this.addressModel.sameAsBilling = true; this.addressModel.state = ''; this.updateAddress(this.addressModel); } } } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @returns void */ ngOnDestroy(): void { const METHOD_NAME: string = 'ngOnDestroy()'; console.log('Destroying the objects initilized.'); this.package = null; this.karmaTestResult = null; this.customerInfo = null; this.addressModel = null; this.stateItems = null; } /** * This function is used to update the address based on the user selection "Same as billing" * @param {boolean} sameAsBilling * @returns void */ switchPpuAddress(sameAsBilling: boolean): void { const METHOD_NAME: string = 'switchPpuAddress()'; if (sameAsBilling === true) { this.addressModel = Object.assign( {}, this.package.ppuAddress); this.addressModel.sameAsBilling = sameAsBilling; this.addressModel.isValidPPU = sameAsBilling; this.addressModel.addressId = this.billingAddressId; } else { if (!this.notSameAddressId) { // check for existing e911 address and updating.. this.addressModel = Object.assign( {}, this.package.ppuAddress); this.addressModel.ppuAddress = true; this.addressModel.sameAsBilling = sameAsBilling; } else { this.addressModel.sameAsBilling = sameAsBilling; this.addressModel.isValidPPU = sameAsBilling; this.addressModel.address2 = ''; this.addressModel.address1 = ''; this.addressModel.city = ''; this.addressModel.state = ''; this.addressModel.zipCode = ''; this.addressModel.addressId = null; } } this.updateAddress(this.addressModel); // we need to update the values } /** * This function is used to validate the address using customerService * @param {any} element contains address. * @returns void */ validateAddress(element: any): void { const METHOD_NAME: string = 'validateAddress()'; if (element && element.valid && (!this.package.ppuAddress || (this.package.ppuAddress['address1'] !== this.addressModel.address1 || this.package.ppuAddress['address2'] !== this.addressModel.address2))) { if (this.addressModel.address1 && this.addressModel.city && this.addressModel.state && this.addressModel.zipCode) { this.addressModel.isValidPPU = false; // this.updateAddress(null); const inputData: any = CartUtils.validateAddressInput(this.addressModel); // this.sharedService.showSpinner(true);//spinner this.customerService.validateAddress(this.storeId, inputData).subscribe(result => { this.karmaTestResult = result; // this.sharedService.showSpinner(false);//spinner if (result.statusCode === Constants.ORDER_STATUS_S) { this.addressModel.isValidPPU = true; this.addressModel.address1 = result.addressLine1; this.addressModel.address2 = result.addressLine2; this.addressModel.city = result.city; this.addressModel.state = result.stateCode; this.addressModel.zipCode = Utils.trimZipCode(result.zipCode); } else { this.sharedService.showErrorMessage('_ERR_INVALID_ADDRESS', false); } }, error => { // this.sharedService.showSpinner(false);//spinner this.sharedService.showErrorMessage(error, true); // console.error('validate address error=>'+ error) }); } } else { this.addressModel.isValidPPU = false; } this.updateAddress(this.addressModel); } /** * This function is used to update the address in customer info after validating the address * @param {any} inputData contains valid address * @returns void */ updateAddress(inputData: any): void { const METHOD_NAME: string = 'updateAddress()'; if (this.customerInfo) { if (this.customerInfo.packages) { let found: boolean = false; for (const pkg of this.customerInfo.packages) { if (pkg.packageId === this.package.packageId && pkg.lineId === this.package.lineId) { found = true; pkg.ppuAddress = inputData; } } if (!found) { this.customerInfo.packages.push( { packageId: this.package.packageId, lineId: this.package.lineId, ppuAddress: inputData }); } } else { this.customerInfo.packages = new Array(); this.customerInfo.packages.push( { packageId: this.package.packageId, lineId: this.package.lineId, ppuAddress: inputData }); } this.checkoutService.setCustomerInfo(this.customerInfo); } } }