/** * @Component Name: E911AddressComponent * @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: 'e911-address-cmp', moduleId: module.id, templateUrl: 'e911-address.component.html' }) export class E911AddressComponent implements OnInit, OnChanges, OnDestroy { private COMPONENT_NAME: string = 'E911AddressComponent'; stateItems: any[]; addressModel: any = { state: '' }; @Input() billingAddressId: number; @Input() package: IPackage; storeId: number; // update storeid customerInfo: any; karmaTestResult: any; sameAddressId: 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]-ShippingComponent.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]-ShippingComponent.ngOnInit()'); this.sameAddressId = (CheckoutUtils.getDeviceNumberService(this.billingAddressId, this.package, 'e911Address', 'addressId')); this.addressModel.e911sameAsBilling = this.sameAddressId; this.addressModel.acceptE911 = false; this.addressModel.isValidE911 = !this.addressModel.e911sameAsBilling; const add = (CheckoutUtils.getDeviceServiceCustInfo(this.customerInfo, this.package, 'e911Address')); if (this.package && this.package.e911Address && !add.isNotSame) { this.addressModel = Object.assign( {}, this.addressModel, this.package.e911Address); } else { if (this.sameAddressId) { if (add.isNotSame) { this.addressModel.e911sameAsBilling = !add.isNotSame; this.addressModel.isValidE911 = add.isValidE911; 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:'); if (this.package.e911Address) { this.addressModel.e911sameAsBilling = false; this.addressModel = Object.assign( {}, this.package.e911Address); if (this.billingAddressId && this.billingAddressId === this.package.e911Address.addressId) { this.addressModel.e911sameAsBilling = true; this.addressModel.state = '' ; this.updateAddress(this.addressModel); } } // acceptE911Disclimer this.acceptE911Disclimer(this.addressModel.acceptE911); } /** * 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 function is used to update the E911 adress based on the selection of same as billing radio button. * @param {boolean} sameAsBilling - indicates 911 address address is same as billing or not. * @returns void */ switchE911Address(sameAsBilling: boolean): void { const METHOD_NAME: string = 'switchE911Address()'; if (sameAsBilling === true) { this.addressModel = Object.assign( {}, this.package.e911Address); this.addressModel.e911sameAsBilling = sameAsBilling; this.addressModel.isValidE911 = sameAsBilling; this.addressModel.addressId = this.billingAddressId; } else { if (!this.sameAddressId) { // check for existing e911 address and updating.. this.addressModel = Object.assign( {}, this.package.e911Address); this.addressModel.isValidE911 = true; this.addressModel.e911sameAsBilling = sameAsBilling; } else { this.addressModel.e911sameAsBilling = sameAsBilling; this.addressModel.isValidE911 = sameAsBilling; this.addressModel.address2 = ''; this.addressModel.address1 = ''; this.addressModel.city = ''; this.addressModel.state = ''; this.addressModel.zipCode = ''; this.addressModel.addressId = null; } } this.updateAddress(this.addressModel); } /** * This function is used to check whether the entered address is valid or invalid. * @param {any} element * @returns void */ validateAddress(element: any): void { const METHOD_NAME: string = 'validateAddress()'; if (element && element.valid && (!this.package.e911Address || (this.package.e911Address['address1'] !== this.addressModel.address1 || this.package.e911Address['address2'] !== this.addressModel.address2))) { if (this.addressModel.address1 && this.addressModel.city && this.addressModel.state && this.addressModel.zipCode) { // this.updateAddress(null); this.addressModel.isValidE911 = false; 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.isValidE911 = 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.isValidE911 = false; } this.updateAddress(this.addressModel); // we need to call in all cases to update the info. } /** * @param {boolean} acceptE911 * @returns void */ acceptE911Disclimer(acceptE911: boolean): void { const METHOD_NAME: string = 'acceptE911Disclimer()'; 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.acceptE911 = acceptE911; } } if (!found) { this.customerInfo.packages.push( { packageId: this.package.packageId, lineId: this.package.lineId, acceptE911: acceptE911 }); } } else { this.customerInfo.packages = new Array(); this.customerInfo.packages.push( { packageId: this.package.packageId, lineId: this.package.lineId, acceptE911: acceptE911 }); } } } /** * @param {any} inputData * @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.e911Address = inputData; } } if (!found) { this.customerInfo.packages.push( { packageId: this.package.packageId, lineId: this.package.lineId, e911Address: inputData }); } } else { this.customerInfo.packages = new Array(); this.customerInfo.packages.push( { packageId: this.package.packageId, lineId: this.package.lineId, e911Address: inputData }); } this.checkoutService.setCustomerInfo(this.customerInfo); } } }