/** * @Component Name: NumberPortingComponent * @Module: Checkout * @Module Functionality : To display the customer information * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, OnInit, Input, AfterViewInit, OnDestroy, OnChanges } from '@angular/core'; import CartUtils from '../../../../shared/cart.utils'; import * as Constants from '../../../../shared/constants'; import { IPackage, INumberPorting } from '../../../../shared/modal/common'; import { SharedService } from '../../../../shared/shared.service'; import { UserService } from '../../../../services/user.service'; import { CheckoutService } from '../../../../services/checkout.service'; import CheckoutUtils from '../../../../shared/checkout.utils'; import { CartService } from './../../../../services/cart.service'; @Component( { selector: 'number-porting-cmp', moduleId: module.id, templateUrl: 'number-porting.component.html' }) export class NumberPortingComponent implements OnInit, OnChanges, OnDestroy { private COMPONENT_NAME: string = 'NumberPortingComponent'; errorMessage: string; error: boolean = false; alertNumberPortingMessage: string; portingModel: any = {}; @Input() packageNumber: number; portPhoneNumber: boolean = false; @Input() package: IPackage; storeId: number; // update storeid customerInfo: any; cartInfo: any; isByos: boolean; constructor( public sharedService: SharedService, public checkoutService: CheckoutService, public cartService: CartService) { } /** * 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()'); // 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()'); // getting the cart info this.cartInfo = this.cartService.getCart(); this.isByos = this.cartInfo.isBYOSOrder; this.portingModel.portPhoneNumber = (CheckoutUtils.getDeviceNumberService(null, this.package, 'numberPorting', 'numberPortingId')); this.portingModel.isValidPorting = this.portingModel.portPhoneNumber; const add = (CheckoutUtils.getDeviceServiceCustInfo(this.customerInfo, this.package, 'numberPorting')); if (this.package && this.package.numberPorting && !add.isNotSame) { this.portingModel = Object.assign( {}, this.portingModel, this.package.numberPorting); } else { if (!this.portingModel.portPhoneNumber) { if (add.isNotSame) { this.portingModel.portPhoneNumber = add.isNotSame; this.portingModel.isValidPorting = add.portPhoneNumber; this.portingModel = Object.assign( {}, this.portingModel, 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.portingModel.portPhoneNumber = false; if (this.package.numberPorting) { this.portingModel.portPhoneNumber = true; this.portingModel = this.package.numberPorting; this.updatePortingNumber(this.portingModel); } } /** * 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 update the phone number based on the user selection * if they select the option to transfet phone number. * @param {boolean} portPhoneNumber valid flag to indiacate user selection * @returns void */ switchPhoneNumber(portPhoneNumber: boolean): void { const METHOD_NAME: string = 'switchPhoneNumber()'; if (portPhoneNumber) { this.error = true; this.alertNumberPortingMessage = this.sharedService.getErrorMessage('DISCOURAGE_SHIPTO_PORTIN_MESSAGE'); this.portingModel = {}; if (this.package.numberPorting) { // check for existing porting this.portingModel = Object.assign( {}, this.package.numberPorting); this.portingModel.isValidPorting = true; this.portingModel.portPhoneNumber = portPhoneNumber; } else { this.portingModel.portPhoneNumber = portPhoneNumber; this.portingModel.isValidPorting = false; } } else { this.portingModel = Object.assign( {}, this.portingModel, this.package.numberPorting); this.portingModel.portPhoneNumber = portPhoneNumber; this.portingModel.isValidPorting = true; // if(this.package.numberPorting) { // this.portingModel = Object.assign( {},this.portingModel,this.package.numberPorting); // } } this.updatePortingNumber( this.portingModel); } /** * This function is used to update account number using updatePortingNumber() when accountNumber is valid * * @param {any} element * @returns void */ validatePortingNumber(element: any): void { const METHOD_NAME: string = 'validatePortingNumber()'; if (element && element.valid && (!this.package.numberPorting || this.package.numberPorting[element.name] !== element.value)) { this.portingModel.isValidPorting = false; if (this.portingModel.phoneNumber && this.portingModel.accountNumber && this.portingModel.accountPin) { this.portingModel.isValidPorting = true; } } else { this.portingModel.isValidPorting = false; } this.updatePortingNumber(this.portingModel); } /** * This function is used to update the modified phone number in the customer info object. * @param {any} inputData valid account number,phone number and pin * @returns void */ updatePortingNumber(inputData: any): void { const METHOD_NAME: string = 'updatePortingNumber()'; 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.numberPorting = inputData; } } if (!found) { this.customerInfo.packages.push( { packageId: this.package.packageId, lineId: this.package.lineId, numberPorting: inputData }); } } else { this.customerInfo.packages = new Array(); this.customerInfo.packages.push( { packageId: this.package.packageId, lineId: this.package.lineId, numberPorting: inputData }); } this.checkoutService.setCustomerInfo(this.customerInfo); } } /** * This function is used to close error message dialog box. * @returns void */ closeError(): void { this.error=false; this.alertNumberPortingMessage = ''; } }