/** * @Component Name: AdjustPriceComponent * @Module: Home * @Module Functionality : To display the pay extra * @Creation Date: November 29,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, OnInit, Output, ViewChild, EventEmitter, OnDestroy } from '@angular/core'; import Utils from '../../../../shared/utils'; import * as Constants from '../../../../shared/constants'; import { ERROR_LIST } from '../../../../shared/error.constants'; import CartUtils from '../../../../shared/cart.utils'; import { IKeyValue } from '../../../../shared/modal/key.value'; import { ADJUST_PRICE_REASONS } from '../../../../shared/form.constants'; import { IPackage, IOrderItemDevice, IOrderItemAccessory } from '../../../../shared/modal/common'; import { Cart } from '../../../../shared/modal/cart'; import { SharedService } from '../../../../shared/shared.service'; import { CartService } from '../../../../services/cart.service'; import { UserService } from '../../../../services/user.service'; import { User } from '../../../../shared/modal/user'; import { PayExtraService } from './../../../../shared/pay-extra-modal.service'; declare var jQuery: any; @Component({ moduleId: module.id, selector: 'pay-extra-cmp', templateUrl: 'pay-extra.component.html', styleUrls: ['pay-extra.component.css'] }) export class PayExtraComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'PayExtraComponent'; visible: boolean = false; visibleAnimate: boolean = false; karmaTestResult: any = {}; storeId: number; errorMessage: string = ''; user: User; @Input() cart: Cart; @Output() canRefreshCart = new EventEmitter(false); @ViewChild('payExtraForm') payExtraForm: any; payExtraModel: any = { 'extraAmount': 0.0 }; maximumPayExtra: number = 0; constructor( public sharedService: SharedService, public userService: UserService, public cartService: CartService, public payExtraService: PayExtraService) { } /** * 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. * @return {void} */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.user = this.userService.getUser(); } /** * 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.cart = null; this.user = null; this.karmaTestResult = null; } ngAfterViewInit(): void { this.show(); } /** * This function used to display the object and also to reset the form. * @return {void} */ public show(): void { const METHOD_NAME: string = 'show()'; this.visible = true; setTimeout(() => this.visibleAnimate = true, 100); this.sharedService.storeId.subscribe((val: number) => {this.storeId = val; }); this.errorMessage = null; this.maximumPayExtra = 0; this.payExtraForm.reset(); if (this.cart) { this.maximumPayExtra = CartUtils.maximumPayExra(this.cart); } } /** * This function is used to hide the object. * @returns {void} */ public hide(): void { const METHOD_NAME: string = 'hide()'; this.visibleAnimate = false; setTimeout(() => this.visible = false, 300); } /** * This function is used to hide the object when nouse event performed * @param {MouseEvent} event contains valid mouse event * @returns {void} */ public onContainerClicked(event: MouseEvent, setModalStatus: string): void { const METHOD_NAME: string = 'onContainerClicked()'; if ((event.target).classList.contains('modal')) { this.hide(); this.closePayExtraModal(setModalStatus); } } /** * This function is used to allow user to pay extra amount on a product. * @param {number} payExtraAmount - price that has to be paid extra * @returns void */ payExtraAmount(payExtraAmount: number): void { const METHOD_NAME: string = 'payExtraAmount()'; if (this.cart && this.cart.packages) { this.sharedService.showSpinner(true); // spinner this.cartService.validateCart(this.storeId, this.cart.orderId, {}).subscribe(result => { this.sharedService.showSpinner(false); // spinner if (result && result.readyForCheckout) { this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Pay extra amount'); const inputData: any = {'payExtraAmount': payExtraAmount }; this.sharedService.showSpinner(true); // spinner this.cartService.payExtraAmount(this.storeId, this.cart.orderId, inputData).subscribe(payExtraResult => { this.karmaTestResult = payExtraResult; this.sharedService.showSpinner(false); // spinner this.canRefreshCart.emit(true); this.hide(); this.closePayExtraModal('close'); }, error => { this.sharedService.showSpinner(false); // spinner this.errorMessage = this.sharedService.getErrorMessage(error); console.error('error=>' + error); }); } else { const errorMessages: any = ERROR_LIST.filter((errorCode) => { return errorCode.key === result.failureReason; }); if (errorMessages && errorMessages.length > 0) { this.errorMessage = this.sharedService.getErrorMessage(errorMessages[0].key); } else { this.errorMessage = this.sharedService.getErrorMessage('_ERR_INVAID_CART'); } } }, error => { this.sharedService.showSpinner(false); // spinner this.errorMessage = this.sharedService.getErrorMessage(error); console.error('error=>' + error) }); } else { this.errorMessage = this.sharedService.getErrorMessage('_ERR_INVAID_CART'); } } public closePayExtraModal(close: string): void{ this.payExtraService.setMessage(close); } }