/** * @Component Name: HomeComponent * @Module: Home * @Module Functionality : To display the products/minicart * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, OnInit, DoCheck, OnDestroy, ViewChild, ElementRef } from '@angular/core'; import { Router } from '@angular/router'; import { IntervalObservable } from 'rxjs/observable/IntervalObservable'; import Utils from '../shared/utils'; import * as Constants from '../shared/constants'; import { Subscription } from 'rxjs/Subscription'; import {takeWhile} from 'rxjs/operators'; import { SharedService } from '../shared/shared.service'; import { CustomerService } from '../services/customer.service'; import { CartService } from '../services/cart.service'; import { CatalogService } from '../services/catalog.service'; import { LookupService } from '../services/lookup.service'; // modal declaration import { INotification, IChannel } from '../shared/modal/common'; import { Logon, User, IPermission, ISalesStats } from '../shared/modal/user'; import { Cart } from '../shared/modal/cart'; import { ErrorComponent } from '../shared/error/error.component'; import { SurveyComponent } from '../shared/survey/survey.component'; import { UserService } from '../services/user.service'; import { CompareService } from '../shared/compare-modal.service'; import { MoreDetailsService } from './../shared/more-details-modal.service'; import { PayExtraService } from './../shared/pay-extra-modal.service'; import CartUtils from 'app/shared/cart.utils'; declare var $: any; @Component({ selector: 'home-cmp', moduleId: module.id, templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent implements OnInit, DoCheck, OnDestroy { private COMPONENT_NAME: string = 'HomeComponent'; excludePaths: string[] = ['search', 'customer', 'coverage', 'fullcart', 'checkout']; showSpinner: boolean = false; showFullSidebar: boolean = true; showCreditOptions: boolean = true; showMinicart: boolean = false; alive: boolean; // used to unsubscribe from the IntervalObservable when OnDestroy is called. storeId: number; // updating storeid errorMessage: string; rtStoreId: number = Constants.STORE_ID_RT; result: any; user: User; // user profile @ViewChild('orderNotesModal') orderNotesModal: any; @ViewChild('clearAllModal') clearAllModal: any; @ViewChild('surveyModal') surveyModal: any; // @ViewChild('scrollToTopRef') scrollToTopRef: ElementRef; compareModalStatus: string = 'close'; compareModalSubscription: Subscription; compareProducts; loanCrp; leaseCrp; deviceFeatureFilters; moreDetailsModalStatus: string = 'close'; product; moreDetailsModalSubscription: Subscription; payExtraModalStatus: string = 'close'; payExtraModalSubscription: Subscription; payExtraCartInfo: any; /** constructor */ constructor( public router: Router, public sharedService: SharedService, public customerService: CustomerService, public cartService: CartService, public catalogService: CatalogService, public lookupService: LookupService, public userService: UserService, public compareService: CompareService, public moreDetailsService: MoreDetailsService, public payExtraService: PayExtraService) { this.alive = true; this.compareModalSubscription = this.compareService.getMessage().subscribe(data => { this.compareModalStatus = data; }); this.moreDetailsModalSubscription = this.moreDetailsService.getMessage().subscribe(data => {this.moreDetailsModalStatus = data;}); this.payExtraModalSubscription = this.payExtraService.getMessage().subscribe(data => {this.payExtraModalStatus = data;}); } /** * 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. Also updateActivePackage function is called to update cart object. * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; console.log('AppComponent.ngOnInit()>>Start'); this.sharedService.updateErrorMessage(null); this.user = this.userService.getUser(); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.sharedService.spinnerStatus.subscribe((val: boolean) => { this.showSpinner = val; }); this.sharedService.showSidebarStatus.subscribe((val: boolean) => { this.showFullSidebar = val; }); this.sharedService.showMincartStatus.subscribe((val: boolean) => { this.showMinicart = val; }); this.sharedService.errorMessage.subscribe((val: string) => { this.errorMessage = val; }); // initialize catalog data console.log('initialize catalog data'); this.sharedService.initializeCatalog(); // get inventory status every subsequent 10 minutes if (this.rtStoreId !== this.storeId) { IntervalObservable.create(60000 * 10).pipe( takeWhile(() => this.alive)) // only fires when component is alive .subscribe(() => { this.catalogService.getInventoryStatus(this.storeId) .subscribe(result => { this.catalogService.setInventoryList(result); this.sharedService.updateInventoryStatus(); }, error => { console.error('error=>' + error); }); }); } console.log('AppComponent.ngOnInit()>>End'); } ngDoCheck(): void { const METHOD_NAME: string = 'ngDoCheck()'; let titlee = window.location.pathname; titlee = titlee.substring(1); // console.log('window.location.pathname>>>>:' + titlee); if (this.excludePaths.some(x => titlee.indexOf(x) > -1)) { this.showCreditOptions = false; } else { this.showCreditOptions = true; if (this.cartService.getCart() || this.customerService.getCustomer()) { this.showCreditOptions = false; } } if(this.compareModalStatus == 'show'){ this.deviceFeatureFilters = this.compareService.deviceFeatureFilters; this.loanCrp = this.compareService.loanCRP; this.leaseCrp = this.compareService.leaseCRP; this.compareProducts = this.compareService.compareProducts; } if(this.moreDetailsModalStatus == 'show'){ this.product = this.moreDetailsService.product; } if(this.payExtraModalStatus == 'show'){ this.payExtraCartInfo = this.payExtraService.cartInfo; } } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @return void */ ngOnDestroy(): void { const METHOD_NAME: string = 'ngOnDestroy()'; this.alive = false; // switches your IntervalObservable off this.excludePaths = null; } /** * This function is used to display or clear ordernotes based on actionName * @param {} actionName contains valid action performed by user. * @returns void */ onTitleAction(actionName): void { const METHOD_NAME: string = 'onTitleAction()'; if (actionName === 'orderNotesModal') { this.showOrderNotesModal(); } else if (actionName === 'clearAllModal') { this.showClearAllModel(); } } /** * This function is used to display the order notes * using show after getting the order number from lookup service or cart from cart service. * @returns void */ showOrderNotesModal(): void { const METHOD_NAME: string = 'showOrderNotesModal()'; const titlee = this.router.url; if (this.lookupService.getOrder() || this.cartService.getCart() || titlee.indexOf('/home/search/order-detail') > -1) { this.orderNotesModal.show(); } else { this.sharedService.showErrorMessage('_ERR_ORDER_NOTES', false); } } /** * This function is used to clear all data using show if the cart is not empty. * @returns void */ showClearAllModel(): void { const METHOD_NAME: string = 'showClearAllModel()'; if (this.lookupService.getOrderList() || this.lookupService.getOrder() || this.customerService.getCustomer() || this.cartService.getCart() || this.customerService.getCustomerModel() || this.lookupService.getOrderSearchModel()) { this.clearAllModal.show(); } else { setTimeout(() => { this.clearAllModal.show(); this.sharedService.setAppDefault(); this.sharedService.clearSessionStorage(false); this.sharedService.showErrorMessage('_ERR_CLEAR_SESSION', false); this.router.navigate(['home/search']); }, 20); } } /** * This function is used to clear all status or session storage from shared service. * @returns void */ clearAllConfirm(): void { const METHOD_NAME: string = 'clearAllConfirm()'; setTimeout(() => { this.clearAllModal.hide(); this.sharedService.showClearAll(true); this.sharedService.showSpinner(true); // spinner this.sharedService.setAppDefault(); // this.sharedService.showMinicart(this.showMinicart =! this.showMinicart); this.sharedService.showSurveyModal(this.surveyModal, this.storeId, this.user.userId); this.sharedService.clearSessionStorage(false); }, 20); } /** * This function is used to update cart details as when cart is updated * @param {number} orderId - orderId * @returns void */ refreshCartSummary(): void { const METHOD_NAME: string = 'refreshCartSummary()'; if (this.payExtraCartInfo.isFinancedOrder && this.payExtraService.isValidCart) { this.sharedService.showSpinner(true); // spinner // this.errorMessage = this.sharedService.showErrorDownPaymentAdjusted(this.payExtraCartInfo.isDownPaymentAdjusted); if(this.payExtraCartInfo.isDownPaymentAdjusted){ this.sharedService.showErrorMessage('_ERR_ERROR_ECALIMIT', false); } this.cartService.calculate(this.storeId, this.payExtraCartInfo.orderId, { actions: 'EIPESTIMATE' }).subscribe(result => { // this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.getCartSummary(); }, error => { this.sharedService.showSpinner(false); // spinner // this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); this.getCartSummary(); }); } else { this.getCartSummary(); } } /** * This function is used to get cart details * @param {number} orderId - orderId * @returns void */ getCartSummary(): void { this.sharedService.showSpinner(true); // spinner this.cartService.getCartSummary(this.storeId, this.payExtraCartInfo.orderId).subscribe(result => { // this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.payExtraCartInfo = CartUtils.replaceRequired(this.payExtraCartInfo, result); // this.errorMessage = this.sharedService.showErrorDownPaymentAdjusted(this.payExtraCartInfo.isDownPaymentAdjusted); if(this.payExtraCartInfo.isDownPaymentAdjusted){ this.sharedService.showErrorMessage('_ERR_ERROR_ECALIMIT', false); } this.cartService.setCart(this.payExtraCartInfo); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } }