import { Component, Input, OnInit, OnChanges, OnDestroy, Output, EventEmitter } from '@angular/core'; import Utils from '../../../shared/utils'; import CartUtils from '../../../shared/cart.utils'; import * as Constants from '../../../shared/constants'; import { IAccessory, IPackage} from '../../../shared/modal/common'; import { SharedService } from '../../../shared/shared.service'; import { CatalogService } from '../../../services/catalog.service'; import { CartService } from '../../../services/cart.service'; import { IKeyValue } from '../../../shared/modal/key.value'; import { INVENTORY_STATUS } from '../../../shared/form.constants'; import { Cart } from '../../../shared/modal/cart'; @Component({ selector: 'related-acc-cmp', moduleId: module.id, templateUrl: 'related-acc.component.html' }) export class RelatedAccComponent implements OnInit, OnChanges, OnDestroy { private COMPONENT_NAME: string = 'RelatedAccComponent'; storeId: number = Constants.STORE_ID_CD; imageBaseUrl: string = Constants.IMAGE_BASE_URL; @Input() deviceSKU: string; cart: Cart; package: IPackage; packageType: string; subscription: any; accessories: Array = new Array(); @Input() financeType: string; @Input() loanCrp: number; @Output() isRelatedAccAddToCart = new EventEmitter(); constructor(public sharedService: SharedService, public catalogService: CatalogService, 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()'; // Start-Switch Channel-Store id update this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; console.log('updated storeid' + this.storeId); }); this.cart = this.cartService.getCart(); // getting cart changes... this.subscription = this.cartService.hasUpdate.subscribe((item) => { if (item) { this.cart = this.cartService.getCart(); } this.updateActivePackage(); }); // End-Switch Channel-Store id update this.updateActivePackage(); } /** * This function is called whenever one or more data-bound input properties change * @returns void */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; if (this.deviceSKU) { this.accessories = new Array() this.sharedService.showSpinner(true); this.catalogService.getAccessoriesByDevice(this.storeId, this.deviceSKU, true).subscribe(result => { this.sharedService.showSpinner(false); // spinner if (result && result.length > 0) { for (const productId of result) { const accessory: IAccessory = Utils.getAccessory(productId); if (accessory) {this.accessories.push(accessory)} } } }, error => { this.sharedService.showSpinner(false); // spinner console.error('error=>' + error); this.sharedService.showErrorMessage(error, true); }); } } /** * 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.accessories = null; this.cart = null; this.package = null; this.subscription.unsubscribe(); } /** * This function is used to get and set update active package. * @returns void */ updateActivePackage(): void { const METHOD_NAME: string = 'updateActivePackage()'; this.package = CartUtils.getCurrentPackage(this.cart); if (this.package) { this.packageType = this.package.packageType; } else { // if there are no active voice package avaible, select first one. this.package = CartUtils.getFirstPackage(this.cart, this.packageType); if (this.package) { this.packageType = this.package.packageType; this.cart.activePackageId = this.package.packageId; this.cart.activeLineId = this.package.lineId; } } } /** * This function is used to get key,valye pairs of inventory status. * @param {string} status * @returns string */ getInventoryStatus(status: string): string { const METHOD_NAME: string = 'getInventoryStatus()'; const statusItems: IKeyValue[] = INVENTORY_STATUS.filter((item) => { return item.key === status; }); if (statusItems && statusItems.length > 0) { return statusItems[0].value; } return '-'; } /** * This function is used to return the state of button for device. * @param {any} accessoryItem contains valid accessory. * @returns string */ buttonState(accessoryItem: any): string { const METHOD_NAME: string = 'buttonState()'; let state: string = Constants.BTN_DISABLE; if (accessoryItem.status !== Constants.OUTOFSTOCK_CODE) { state = Constants.BTN_ADD; } return state; } /** * this function is used to add accessory to cart. * @param {IAccessory} accessory contains valid accessory. * @returns void */ addAccessory(accessory: IAccessory): void { const METHOD_NAME: string = 'addAccessory()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Adding accessory to cart'); console.log('[start]- adding accessory to cart.'); // making sure there was cart in progress if (this.package) { // US321974:As a DASH user I want to see EIP pricing for associated Accessories listed in an EIP device's details so that I can sell more accessories // const inputData: any = CartUtils.createAccessoryInput(this.packageType, accessory, Constants.FINANCE_TYPE_FRP, null); const inputData: any = CartUtils.createAccessoryInput(this.packageType, accessory, this.financeType, this.loanCrp); this.sharedService.showSpinner(true); // spinner this.cartService.createAccessory(this.storeId, this.cart.orderId, this.package.packageId, this.package.lineId, inputData ).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.cart = CartUtils.replaceRequired(this.cart, result); this.isRelatedAccAddToCart.emit(true); this.cartService.setCart(this.cart); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } else { console.log('No active package...') } console.log('[end]- adding accessory to cart.'); } }