/** * @Component Name: MinicartComponent * @Module: Home * @Module Functionality : To display the minicart * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, ViewChild, Input, OnInit, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; import * as Constants from '../../shared/constants'; import { Cart } from '../../shared/modal/cart'; import CartUtils from '../../shared/cart.utils'; import { SharedService } from '../../shared/shared.service'; import { CartService } from '../../services/cart.service'; @Component({ moduleId: module.id, selector: 'minicart-cmp', templateUrl: 'minicart.component.html', styleUrls: ['minicart.component.css'] }) export class MinicartComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'MinicartComponent'; checkoutResponse: any = {}; storeId: number; // update storeid cart: Cart; refTypeNew: string = Constants.REF_TYPE_NEW; refTypeReturn: string = Constants.REF_TYPE_RETURN; refTypeExchange: string = Constants.REF_TYPE_EXCHANGE; subscription: any; constructor( public router: Router, 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() { const METHOD_NAME: string = 'ngOnInit()'; // Start-Switch Channel-Store id update this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); // End-Switch Channel-Store id update this.cart = this.cartService.getCart(); // to update the state tax for new customer below subscription is required. this.subscription = this.cartService.hasUpdate.subscribe((item) => { if (item) { this.cart = this.cartService.getCart(); } // if(this.cart && this.cart.customer) this.customerTaxUpdate(); }); } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. */ ngOnDestroy() { const METHOD_NAME: string = 'ngOnDestroy()'; this.cart = null; this.subscription.unsubscribe(); } /** This function is used to restrict user from navigating to shopping cart if cart is empty. * @returns void */ showShoppingCart(): void { const METHOD_NAME: string = 'showShoppingCart()'; if (this.cart) { this.router.navigate(['/home/fullcart']); } else { this.sharedService.showErrorMessage('_ERR_EMPTY_CART', false); } } }