import { Component, OnInit, Input, OnChanges, OnDestroy } from '@angular/core'; import { IKeyValue } from '../../../../shared/modal/key.value'; import CartUtils from '../../../../shared/cart.utils'; import * as Constants from '../../../../shared/constants'; import { Cart } from '../../../../shared/modal/cart'; import { IPackage } from '../../../../shared/modal/common'; import { SharedService } from '../../../../shared/shared.service'; import { CartService } from '../../../../services/cart.service'; @Component({ moduleId: module.id, selector: 'return-cmp', templateUrl: 'return.component.html', styleUrls: ['./return.component.css'] }) export class ReturnComponent implements OnInit, OnDestroy, OnChanges { private COMPONENT_NAME: string = 'ReturnComponent'; packageTypes: string[] = [ Constants.PACKAGE_TYPE_VOICE, Constants.PACKAGE_TYPE_DATA, Constants.PACKAGE_TYPE_PREPAID, Constants.PACKAGE_TYPE_ACCESSORY ]; imageBaseUrl: string = Constants.IMAGE_BASE_URL; storeId: number; @Input() relatedOrderId: number; returnCart: any; constructor(public sharedService: SharedService, 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. * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.returnCart = this.cartService.getReturnCart(); } /** * This function is called whenever one or more data-bound input properties change * @returns void */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; this.returnCart = this.cartService.getReturnCart(); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); if (this.relatedOrderId && !this.returnCart) { this.sharedService.showSpinner(true); // spinner this.cartService.getCartSummary(this.storeId, this.relatedOrderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.returnCart = result; this.cartService.setReturnCart(this.returnCart); if (this.returnCart && this.returnCart.packages) { for (const item of this.returnCart.packages) { item.showPackage = false; } this.returnCart.packages[0].showPackage = true; } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error) }); } } /** * 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()'; this.returnCart = null; this.packageTypes = null; } /** * This function is used to get list of packages that matches package type * @param {string} type - package type * @returns IPackage - list of packages that matches package type */ getReturnPackages(type: string): IPackage[] { const METHOD_NAME: string = 'getReturnPackages()'; // console.log('package type:' + type); return this.returnCart.packages.filter((pkg) => { return (pkg.packageType === type) }); } }