import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnChanges, OnDestroy, OnInit } from '@angular/core'; import {Subscription} from 'rxjs/Subscription'; import {select, Store} from '@ngrx/store'; import {distinctUntilChanged, filter} from 'rxjs/operators'; import {CookiesService} from '@hdca/fe-angular-shared'; import {PetaState} from '../reducers'; import * as constant from '@hdca/fe-angular-shared'; import { PETAListRequestState, PETARequestState } from '../reducers'; import { selectPETAListDataState, selectPETADataState } from '../selectors/peta.selectors'; @Component ({ selector: 'personalized-eta', templateUrl: './peta.container.html', changeDetection: ChangeDetectionStrategy.OnPush }) export class PersonalizedETAContainer implements OnInit, OnChanges, OnDestroy { @Input() productCode: string; @Input() fulfillmentType: string; @Input() genericETAValue: string; @Input() genericETAUnit: string; @Input() isInStoreList: boolean; @Input() storeName: string; petaSubscription: Subscription; petaListSub: Subscription; estimateDelivery: string; genericETAValueWithUnit: string; lang: string; dash: string; constructor(private petaStore: Store, private cookiesService: CookiesService, private changeDetectorRef: ChangeDetectorRef) { this.lang = this.cookiesService.getCookie(constant.pageCookies.LANG); this.dash = this.lang === constant.utils.EN ? '–' : 'à'; } ngOnInit() { if (!this.isInStoreList) { this.getSubscription(); } else { this.getPETAStoreSubscription(); } } ngOnChanges() { const cleanGenericPETAValue = (this.genericETAValue) ? this.genericETAValue.replace('–', this.dash) : ''; this.genericETAValueWithUnit = cleanGenericPETAValue ? cleanGenericPETAValue.concat(' ').concat(this.genericETAUnit) : ''; this.estimateDelivery = this.genericETAValueWithUnit; } ngOnDestroy() { if (!this.isInStoreList) { this.petaSubscription.unsubscribe(); } else { this.petaListSub.unsubscribe(); } } getSubscription() { this.petaSubscription = this.petaStore.pipe( select(selectPETADataState), distinctUntilChanged(), filter((peta: PETARequestState) => { const petaItems = (peta.SOURCING_RESULT.SOURCING_RESULTS && peta.SOURCING_RESULT.SOURCING_RESULTS.length > 0) ? peta.SOURCING_RESULT.SOURCING_RESULTS[0] : null; if (petaItems && petaItems.CART_ITEMS && petaItems.CART_ITEMS.length > 0) { return true; } else { return false; } }), ).subscribe((peta: PETARequestState) => { this.getEstimateDeliveryTime(peta.SOURCING_RESULT); }, err => { console.log('peta call failed:', err); }); } getPETAStoreSubscription() { this.petaListSub = this.petaStore.pipe( select(selectPETAListDataState), filter((peta: PETAListRequestState) => { return !peta.loading; }), distinctUntilChanged(), ).subscribe((petaList: any) => { this.getETAStoreList(petaList.petaList); }); } getEstimateDeliveryTime(peta) { try { this.estimateDelivery = this.genericETAValueWithUnit; const petaItems = (peta.SOURCING_RESULTS && peta.SOURCING_RESULTS.length > 0) ? peta.SOURCING_RESULTS[0] : {}; // filter PETA state this.getSAPPETAValue(petaItems); this.changeDetectorRef.markForCheck(); } catch (error) { console.log('PETA rendered error: ', error); } } getETAStoreList(petaList) { try { this.estimateDelivery = this.genericETAValueWithUnit; if (petaList && petaList.length > 0) { petaList.forEach((peta) => { const petaItems = (peta.SOURCING_RESULT.SOURCING_RESULTS && peta.SOURCING_RESULT.SOURCING_RESULTS.length > 0) ? peta.SOURCING_RESULT.SOURCING_RESULTS[0] : []; // filter PETA state this.getSAPPETAValue(petaItems, true); }); } else { this.estimateDelivery = this.genericETAValueWithUnit; } } catch (error) { console.log('PETA in store list rendered error: ', error); } } getSAPPETAValue(petaItems, isInStoreList: boolean = false) { if (petaItems && petaItems.CART_ITEMS && petaItems.CART_ITEMS.length > 0) { let cartItem; if (isInStoreList || this.storeName) { cartItem = petaItems.CART_ITEMS.find((pItem) => { const storeName = pItem.EXTERNAL_ID.split('-')[1]; return (this.fulfillmentType === pItem.FULFILLMENT_TYPE && this.storeName === storeName); }); } else { cartItem = petaItems.CART_ITEMS.find((pItem) => { const sapOriginProductCode = pItem.EXTERNAL_ID.split('-')[0]; const sapProductCode = +sapOriginProductCode; return (this.fulfillmentType === pItem.FULFILLMENT_TYPE && this.productCode === sapProductCode.toString()); }); } // get PETA value and unit if (cartItem) { const sapPETAValue = cartItem.FULFILLMENT_ETA; const sapPETAUnit = cartItem.FULFILLMENT_UNIT; this.estimateDelivery = (sapPETAValue) ? sapPETAValue.replace(',', ' ' + this.dash + ' ').concat(' ').concat(sapPETAUnit.toLowerCase()) : this.genericETAValueWithUnit; } else if (!isInStoreList && !cartItem) { this.estimateDelivery = this.genericETAValueWithUnit; } } else { this.estimateDelivery = this.genericETAValueWithUnit; } } }