/** * @Component Name: AccessoriesComponent * @Module: Home * @Module Functionality : To display the accessories * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, ChangeDetectionStrategy, OnInit, DoCheck, OnDestroy } from '@angular/core'; import Utils from '../../shared/utils'; import CartUtils from '../../shared/cart.utils'; import * as Constants from '../../shared/constants'; import { SORT_BY_OPTIONS, INVENTORY_STATUS } from '../../shared/form.constants'; import { IKeyValue } from '../../shared/modal/key.value'; import { IAccessory, IAccessoryItem, IFilterList, IFilter, IPackage, IDevice } from '../../shared/modal/common'; import { User } from '../../shared/modal/user'; import { Cart } from '../../shared/modal/cart'; import { Customer, ICreditProfile } from '../../shared/modal/customer'; import { SharedService } from '../../shared/shared.service'; import { UserService } from '../../services/user.service'; import { CartService } from '../../services/cart.service'; import { CustomerService } from '../../services/customer.service'; import { CatalogService } from '../../services/catalog.service'; import { MarketingService } from '../../services/marketing.service'; import { CompareService } from '../../shared/compare-modal.service'; import { MoreDetailsService } from '../../shared/more-details-modal.service'; declare var jQuery: any; @Component( { moduleId: module.id, selector: 'accessories-cmp', templateUrl: 'accessories.component.html', styleUrls: ['accessories.component.css'], // changeDetection: ChangeDetectionStrategy.OnPush }) export class AccessoriesComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'AccessoriesComponent'; storeId: number; // update storeid creditType: string = Constants.DEFAULT_CREDIT; loanCrp: number = Constants.LOAN_CRP_DEFAULT; leaseCrp: number = Constants.LEASE_CRP_DEFAULT; financeTypeFull: string = Constants.FINANCE_TYPE_FRP; financeTypeLoan: string = Constants.FINANCE_TYPE_LOAN; financeTypeLease: string = Constants.FINANCE_TYPE_LEASE; groupByCount: number = jQuery(window).width() <= 991 ? 2 : 3; cardsPerPage: number = Constants.CARDS_PER_PAGE; // filters expand and collapse flags showBrandFilter: boolean = false; showDeviceTypeFilter: boolean = false; showFeatureFilter: boolean = false; // header section veriables financeType: string = Constants.FINANCE_TYPE_FRP; // for finance type slider searchText: string = ''; // search text accSuggestions: Array = []; suggestionItems: Array = []; sortByKey: string = ''; // sort by key sortByOptions: IKeyValue[]; // sort by price and alphabet // body section veriables imageBaseUrl: string = Constants.IMAGE_BASE_URL; accessories: IAccessory[]; wholeAccessoryList: IAccessory[]; filters: IFilterList[]; accessoryTypeFilters: IFilter[]; // sorted accessories for display sortedAccessories: Array>; // pagination allItems: any[]; // array of all items to be paged pager: any = {};  // pager object pagedItems: any[]; // paged items // added for expand and collapse expandedProductId: number; // adding item to cart functionality packageType: string; user: User; customer: Customer; creditProfile: ICreditProfile; subscription: any; cart: Cart; // getting active package item package: IPackage; isAALOrder: boolean = false; canFinance: boolean = true; isExchangeOrder: boolean = false; disableAddToCart: boolean = false; isFinanceOrder: boolean = false; isEIPOnlyOrder: boolean = false; isJUMPOnlyOrder: boolean = false; isNCFPlanInCart: boolean = false; isOnlyAccPackage: boolean = false; activePackageId: number = -1; // @mani ==> reference variable for active packageId activeLineId: number = -1; // @mani ==> reference variable for active lineId modalStatus: boolean = false; mfPartNumber: string; productName: string; imageUrl: string; categoryId: number; // @mani parameters for Accessories Filter Based on manufacturer and Device Selected devices: IDevice[]; deviceFilters: any[]; deviceBrands: any; deviceItems: any[] = []; deviceSKU: string = ''; deviceBrandSelect: any = ''; selectedDevicesArray: any[] = []; showDeviceBrands: boolean = false; devicesFilterListArray: any[] = []; previousSelectedIndex: number; karmaTestResult: any; rtStoreId: number = Constants.STORE_ID_RT; // @mani parameters for Compare bannerName: string = Constants.BANNER_ACCESSORIES; compareProducts: Array = new Array(); constructor( public sharedService: SharedService, public userService: UserService, public cartService: CartService, public catalogService: CatalogService, public customerService: CustomerService, public marketingService: MarketingService, public compareService: CompareService, public moreDetailsService: MoreDetailsService) { } /** * 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 the user profile, customer information * Master accessories list and accessory filters * @return void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; // this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Loading accessories'); // @mani => Seeting the Category Id this.categoryId = Constants.CATEGORY_ID_ACCESSORY; // Start-Switch Channel-Store id update this.sharedService.updateErrorMessage(null); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.sortByOptions = SORT_BY_OPTIONS; if (this.sortByOptions) { this.sortByKey = this.sortByOptions[0].key; } this.sharedService.creditChoice.subscribe((val: string) => { this.creditType = val; if ( this.creditType && this.creditType === Constants.NOCREDIT_CHECK) { this.financeType = Constants.FINANCE_TYPE_FRP; } this.updateActivePackage(); }); this.groupByCount = Utils.groupByProductCount(); this.cardsPerPage = Utils.totalCardsPerPage(); this.user = this.userService.getUser(); // getting current cart this.customer = this.customerService.getCustomer(); this.cart = this.cartService.getCart(); // getting cart changes... this.subscription = this.cartService.hasUpdate.subscribe((item) => { if (item) { this.cart = this.cartService.getCart(); } this.updateActivePackage(); }); console.log('getting the master accessories'); this.accessories = this.catalogService.getAccessories(); if (!this.accessories) { console.log('AccessoryListComponent.ngOnInit()>>Getting the master accessories.'); this.sharedService.initializeCatalog(); } this.wholeAccessoryList = this.accessories; this.filters = this.catalogService.getAccessoryFiletrs(); if (!this.filters) { console.log('AccessoryListComponent.ngOnInit()>>Getting the master accessory filters.'); this.sharedService.initializeCatalog(); } else { this.resetAccessoryFilters(); } // @mani Getting the devices Array this.devices = this.catalogService.getGSMDevices(); if (!this.devices) { console.log('VoiceDevicesComponent.ngOnInit()>>Getting the master devices.'); this.sharedService.initializeCatalog(); } // @mani Getting the Devices Filter Array this.deviceFilters = this.catalogService.getGSMDeviceFiletrs(); if (!this.deviceFilters) { console.log('VoiceDevicesComponent.ngOnInit()>>Getting the master device filetrs.'); this.sharedService.initializeCatalog(); } // console.log('Devices Filter Litst:'+ JSON.stringify(this.deviceFilters)); if (this.deviceFilters && this.deviceFilters.length !== 0) { this.deviceBrands = (this.deviceFilters.filter(filterItem => { return filterItem.type === Constants.DEVICE_GRP_NAME_MNFR; })[0]); } // get active package this.updateActivePackage(); jQuery(document).click(function (e) { if (e.target.id === 'search1') { jQuery('#suggestions').show(); } else { jQuery('#suggestions').hide(); } }); } /** * 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.accessories = null; this.filters = null; this.customer = null; this.cart = null; this.package = null; this.accessoryTypeFilters = null; this.subscription.unsubscribe(); this.pager = null; this.creditProfile = null; this.karmaTestResult = null; this.suggestionItems = null; this.sortByOptions = null; this.sortedAccessories = null; this.allItems = null; this.pagedItems = null; this.devices = null; this.deviceFilters = null; this.deviceBrands = null; this.deviceItems = null; this.deviceBrandSelect = null; this.selectedDevicesArray = null; this.devicesFilterListArray = null; } /** * This function is used to update the package including package type, active package id, * active line id.Also getting the customer details including credit profile and credit types * @return void */ updateActivePackage(): void { const METHOD_NAME: string = 'updateActivePackage()'; // getting active package from cart 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; } } if (this.packageType === Constants.PACKAGE_TYPE_VOICE || this.packageType === Constants.PACKAGE_TYPE_DATA) { // if(this.package.device) this.sortByDevice(this.package.device.deviceSKU); if (this.package && this.package.device) { const deviceInfo = this.package.device.name + ',' + this.package.device.deviceSKU; this.createSelectedDevicesArray(deviceInfo); // this.sortByDevice(this.package.device.deviceSKU); } } // initilizing local veriables this.isFinanceOrder = CartUtils.isFinanceOrder(this.cart); this.isEIPOnlyOrder = CartUtils.isEIPOnlyOrder(this.cart); this.isJUMPOnlyOrder = CartUtils.isJUMPOnlyOrder(this.cart); this.isNCFPlanInCart = CartUtils.isNCFPlanInCart(this.cart); this.isOnlyAccPackage = false; if (((!this.package || (this.package && this.package.packageType === Constants.PACKAGE_TYPE_ACCESSORY))&& !this.isExchangeOrder) ||(this.isExchangeOrder && !this.sharedService.isExchangeEipPackage)) { this.isOnlyAccPackage = true; } this.isExchangeOrder = (this.cart && this.cart.refType === Constants.REF_TYPE_EXCHANGE) ? true : false; // getting the customer if (!this.customer && this.cart && this.cart.customer) { this.customer = this.cart.customer; } if (this.customer && this.customer.creditProfile) { this.creditProfile = this.customer.creditProfile; this.loanCrp = this.creditProfile.loanCrp ? this.creditProfile.loanCrp : Constants.LOAN_CRP_DEFAULT; this.leaseCrp = this.creditProfile.leaseCrp ? this.creditProfile.leaseCrp : Constants.LEASE_CRP_DEFAULT; } else { if (this.creditType) { this.loanCrp = Utils.getLoanCrp(this.creditType); this.leaseCrp = Utils.getLeaseCrp(this.creditType); } } if (this.creditProfile && this.creditProfile.accountNumber) { this.isAALOrder = !this.isExchangeOrder ? true : false; this.canFinance = (!this.creditProfile.isExtendedPaySchedule && !this.creditProfile.hideFinance) ? true : false; // checking the enable/diable add to cart button for aal path if ( this.creditProfile.taxTreatment === Constants.MIXED_TAX_TREATMENT || this.creditProfile.isDelinquent || this.creditProfile.effectiveDate || (this.creditProfile.accountType === Constants.ACCOUNT_TYPE_I && this.creditProfile.accountSubType === Constants.ACCOUNT_SUB_TYPE_S)) { this.disableAddToCart = true; if (this.creditProfile.accountType === Constants.ACCOUNT_TYPE_I && this.creditProfile.accountSubType === Constants.ACCOUNT_SUB_TYPE_S ) { this.accessories = null; this.sharedService.showErrorMessage('_ERR_SOLE_PROPRIETOR', false); } else if (this.creditProfile.isDelinquent) { this.sharedService.showErrorMessage('_ERR_DELINQUENT_CUSTOMER', false); } else if (this.creditProfile.taxTreatment === Constants.MIXED_TAX_TREATMENT || this.creditProfile.effectiveDate) { this.sharedService.showErrorMessage('_ERR_FUTURED_DATED_TAX_TREATMENT', false); } } } // applying the custom sorting this.applyCustomSorting(); } /** * This function used to add the accessory to shopping cart * @param {IAccessory} accessory - Object contains accessory information * @return 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.'); // checking preprocess checks this.addItemPreprocess(accessory); // adding product into cart/package if (this.cart && this.package && !CartUtils.isBYOSOnlyOrder(this.cart)) { this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Adding accessory to cart:' + this.cart.orderId); const packageType = this.package ? this.package.packageType : Constants.PACKAGE_TYPE_ACCESSORY; const crp: number = (this.financeType && this.financeType === this.financeTypeLoan) ? this.loanCrp : this.leaseCrp; const inputData: any = CartUtils.createAccessoryInput(packageType, accessory, this.financeType, crp); if (this.package.returnOrderItemId) { inputData.originalOrderItemId = this.package.returnOrderItemId; inputData.orderRefType = Constants.REF_TYPE_EXCHANGE; } this.sharedService.showSpinner(true); // spinner this.cartService.createAccessory(this.storeId, this.cart.orderId, this.package.packageId, this.package.lineId, inputData ).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.cart = CartUtils.replaceRequired(this.cart, result); this.cartService.setCart(this.cart); // this.selectSuggestionItem(null); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } else { if (CartUtils.isBYOSOnlyOrder(this.cart)) { this.sharedService.showErrorMessage('_ERR_BYOS_ONLY_ORDER', false); } } console.log('[end]- adding accessory to cart.'); } /** * This function is used to prepare the cart before adding the accessory to cart * @param {IAccessory} accessory - Object contains accessory information * @return void */ addItemPreprocess(accessory: IAccessory): void { const METHOD_NAME: string = 'addItemPreprocess()'; console.log('[start]- preprocess of adding item to cart.'); if (!this.cart) { // making sure there was cart in progress this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Creating a cart'); const inputData: any = CartUtils.createCartInput(this.storeId, this.user); this.sharedService.showSpinner(true); // spinner this.cartService.createCartDetails(this.storeId, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.cart = result; // calling back add to accessory method again... this.addAccessory(accessory); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); }else if(this.cart && !this.cart.customer && this.customer){ this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Associating the customer to cart:' + this.cart.orderId); //adding order notes if (this.customer.orderNotes) { this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, this.customer.orderNotes); this.customer.orderNotes = null; } this.customer.orderId = this.cart.orderId; this.sharedService.showSpinner(true); // spinner this.customerService.createCustomer(this.storeId, this.customer).subscribe(createCustomerResult => { this.sharedService.showSpinner(false); // spinner this.cart.customer = Object.assign({}, this.customer); this.cart.customer.addressId = createCustomerResult.addressId; this.customer = null; this.customerService.setCustomer(null); this.cartService.setCart(this.cart); // calling back add to accessory method again... this.addAccessory(accessory); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); }else if (this.cart && !this.package) { // making sure there was active package this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Creating a package to cart:' + this.cart.orderId); const inputData = CartUtils.createPackageInput(Constants.PACKAGE_TYPE_ACCESSORY, 1, null); this.sharedService.showSpinner(true); // spinner this.cartService.createPackageDetails(this.storeId, this.cart.orderId, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner const packages: IPackage[] = result; if (!this.packageType) { this.packageType = Constants.PACKAGE_TYPE_ACCESSORY; } for (const pkg of packages) { pkg.packageType = this.packageType; } // setting active package#&line# this.package = packages[0]; this.cart = CartUtils.addPackage(this.cart, packages); this.cartService.setCart(this.cart); // calling back to add to accessory again this.addAccessory(accessory); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } } /** * This function is used to remove the accessory from cart * @param {IAccessory} accessory - Object contains accessory information * @return void */ removeAccessory(accessory: IAccessory): void { const METHOD_NAME: string = 'removeAccessory()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Remove accessory from cart:' + this.cart.orderId); console.log('[start]- removing product from cart.'); // adding product into cart/package if (this.cart && this.package && this.package.accessories) { const accessoryItem: IAccessoryItem = accessory.items[0]; const remProduct: any = this.package.accessories.filter((item) => { return item.accessorySKU === accessoryItem.accessorySKU; }); if (remProduct && remProduct.length > 0) { this.sharedService.showSpinner(true); // spinner this.cartService.deleteAccessory(Constants.STORE_ID_CD, this.cart.orderId, this.package.packageId, this.package.lineId, remProduct[0].orderItemId ).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.cart = CartUtils.replaceRequired(this.cart, result); this.cartService.setCart(this.cart); // this.selectSuggestionItem(null); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } } console.log('[end]- removing product from cart.'); } /** * This function is used to apply the custom sorting of products * @return void */ applyCustomSorting(): void { const METHOD_NAME: string = 'applyCustomSorting()'; if (this.accessories && this.accessories.length > 0) { // support products for full price/loan/lease const crp: number = this.financeType === Constants.FINANCE_TYPE_LOAN ? this.loanCrp : this.leaseCrp; let sortList: any = Utils.keepFinanceItems(this.accessories, this.financeType, crp); // let sortList:any = Utils.sortByFinance(this.accessories,this.financeType, crp); sortList = Utils.serachFilter(sortList, this.filters); sortList = Utils.serachSortBy(sortList, this.sortByKey); this.allItems = Utils.serachProducts(sortList, this.searchText); // initilizing the compare products and expand product# if (this.allItems && this.allItems.length > 0) { this.expandedProductId = null; // this.compareProducts = new Array(); if (this.compareProducts.length < 1) { for (const product of this.allItems) { product.canCompare = false; } } } // initialize to page 1 this.setPage((this.pager && this.pager.currentPage) ? this.pager.currentPage : 1); } } /** * This function is used to get the index for pagination * @param {number} page - variable contains the page number * @return void */ setPage(page: number): void { const METHOD_NAME: string = 'setPage()'; if (page < 1 || page > this.pager.totalPages) { return; } if (this.allItems && this.allItems.length > 0) { // get pager object from service this.pager = Utils.getPager(this.allItems.length, page, this.cardsPerPage); // get current page of items this.pagedItems = this.allItems.slice(this.pager.startIndex, this.pager.endIndex + 1); // grouping 3 plans per row this.sortedAccessories = Utils.groupProducts(this.pagedItems, this.groupByCount); } else { this.sortedAccessories = null; } } /** * This function is used to get the status of button which is enabled/disabled * based on accessories out of stock * @param {IAccessoryItem} accessoryItem - Object contains the accessory status * @return string */ buttonState(accessoryItem: IAccessoryItem): string { const METHOD_NAME: string = 'buttonState()'; let state: string = Constants.BTN_DISABLE; if (accessoryItem.status !== Constants.OUTOFSTOCK_CODE) { state = Constants.BTN_ADD; // if (this.cart && this.package && this.package.accessories) { // //console.log(this.package.device); // let accessories: any = this.package.accessories.filter((accessory) => { // return this.financeType === accessory.financeType && accessory.accessorySKU === accessoryItem.accessorySKU; // }); // if (accessories && accessories.length > 0) { // state = Constants.BTN_REMOVE; // } // } } return state; } /** * This function is used to get key,valye pairs of inventory status. * @param {IAccessoryItem} accesory item * @returns boolean */ hasOffer(accessoryItem: IAccessoryItem): boolean { return Utils.hasOffer( accessoryItem ); } /** * This function is used to get the inventory status * @param {string} status - variable contains the item status * @return 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 sort by device * @param {string} deviceSKU - variable contains the device SKU * @return void */ sortByDevice(deviceSKU: string): void { const METHOD_NAME: string = 'sortByDevice()'; const checkDeviceSKUAvailability = this.devicesFilterListArray.filter(deviceList => { return deviceList.deviceSKU === deviceSKU; }); // @mani ==> Checking if the data is already available to avoid api call for the same deviceSKU again if (checkDeviceSKUAvailability.length !== 0) { this.accessories = checkDeviceSKUAvailability[0].accessoryList; this.resetAccessoryFilters(); this.applyCustomSorting(); } else if(this.package && this.package.device && this.package.device.isSimOnly){ this.accessories = this.wholeAccessoryList; this.resetAccessoryFilters(); this.applyCustomSorting(); } else { this.sharedService.showSpinner(true); // spinner this.catalogService.getAccessoriesByDevice(this.storeId, deviceSKU, false).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner if (result && result.length) { this.accessories = this.catalogService.getAccessories(); this.accessories = this.accessories.filter((item) => { // return (result.includes(item.productId)); return result.indexOf(item.productId) > -1; }); this.devicesFilterListArray.push( { 'deviceSKU': deviceSKU, 'accessoryList': this.accessories }); this.resetAccessoryFilters(); this.applyCustomSorting(); } }, error => { this.selectedDevicesArray.pop(); if (this.selectedDevicesArray.length !== 0) { this.selectedDevicesArray[this.previousSelectedIndex].isChecked = true; } this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } } /** * This function is used to reset accessory filters * @return void */ resetAccessoryFilters(): void { const METHOD_NAME: string = 'resetAccessoryFilters()'; if (this.filters) { this.filters.forEach((element) => { if (element.type === Constants.ACCESSORY_GRP_NAME_TYPE) { this.accessoryTypeFilters = element.items; } }); if (this.accessories) { // this.filters = new Array(); this.accessoryTypeFilters = Utils.matchFiltersProducts(this.accessoryTypeFilters, this.accessories); // if (this.accessoryTypeFilters) { // this.filters.push( {type: Constants.ACCESSORY_GRP_NAME_TYPE, items: this.accessoryTypeFilters}); // } } } } /** * This function is used to get device filters in an array. * @returns Array */ get deviceTypeFilters(): Array { const METHOD_NAME: string = 'deviceTypeFilters()'; if (this.filters) { for (const item of this.filters) { if (item.type === Constants.DEVICE_GRP_NAME_TYPE) { return item.items; } } } return null; } /** * This function is used to get device features in an array. * @returns Array */ get deviceFeatureFilters(): Array { const METHOD_NAME: string = 'deviceFeatureFilters()'; if (this.filters) { for (const item of this.filters) { if (item.type === Constants.DEVICE_GRP_NAME_FEATURE) { return item.items; } } } return null; } /** * This function determines whether any of the devices are selected or not. * @returns boolean */ atleastOneChecked(): boolean { const METHOD_NAME: string = 'atleastOneChecked()'; if (this.filters && this.filters.length > 0) { for (const filter of this.filters) { if (filter && filter.items) { for (const item of filter.items) { if (item.isChecked) { return true; } } } } } return false; } /** * This function is used to clear all the filters. * @returns void */ clearAllFilters(): void { const METHOD_NAME: string = 'clearAllFilters()'; for (const filter of this.filters) { for (const item of filter.items) { item.isChecked = false; } } if (this.sortByOptions) { this.sortByKey = this.sortByOptions[0].key; } this.searchText = ''; this.financeType = Constants.FINANCE_TYPE_FRP; // update sorted accessories this.applyCustomSorting(); } /** * This function is used to display the details of particular item. * @param {number} productId - valid productId of an accessory. * @returns void */ expandDetails(productId: number): void { const METHOD_NAME: string = 'expandDetails()'; if (this.expandedProductId && this.expandedProductId === productId) { this.expandedProductId = null; } else { this.expandedProductId = productId; } } /** * This function is used to compare two or more products * @param {boolean} isChecked - variable contains the boolean value of products checked to compare * @param {IAccessory} product - Object contains the product details * @return void */ updateCompareProducts(isChecked: boolean, product: IAccessory): void { const METHOD_NAME: string = 'updateCompareProducts()'; if (isChecked) { if (this.compareProducts) { if (this.compareProducts.length < Constants.MAXIMUM_COMPARE) { product.canCompare = true; this.compareProducts.push(product); } else { product.canCompare = false; // console.log('show excceed model'); } } else { this.compareProducts = new Array(); this.compareProducts.push(product); } jQuery('.checked-status').addClass('visible'); } else { product.canCompare = false; if (this.compareProducts) { // removing item from compare product list const index = this.compareProducts.indexOf(product, 0); if (index > -1) { this.compareProducts.splice(index, 1); } } } } /** * This function is used to enable or disable the compare button. * @param {IAccessory} product - individual accessory. * @returns boolean */ disableCompare(product: IAccessory): boolean { const METHOD_NAME: string = 'disableCompare()'; if (!product.canCompare) { if (this.compareProducts && this.compareProducts.length === Constants.MAXIMUM_COMPARE) { return true; } } return false; } /** * Getting Device list based on the manufacturer selected * @param {any} productIds - variable contains the product ids * @return void */ getDeviceItems(productIds: any): void { const METHOD_NAME: string = 'getDeviceItems()'; this.deviceItems = []; if (productIds.length !== 0) { this.devices.forEach(item => { if (productIds.indexOf(item.productId) > -1) { item.items.forEach(subItem => { if(!subItem.isByos && subItem.name.indexOf('SIM')=== -1) { this.deviceItems.push(subItem); } }); } }); } } /** * This function is used to create the array of selected devices. * @param {any} deviceSKU - deviceSKU of the selected device. * @returns void */ createSelectedDevicesArray(deviceSKU: any): void { const METHOD_NAME: string = 'createSelectedDevicesArray()'; this.sharedService.addLogging(this.COMPONENT_NAME,  METHOD_NAME,  'Adding the selected device for device filter:'); const objToCreateJson = {}; const splittedItem = deviceSKU.split(','); const deviceSKUavailabilityCheck = this.selectedDevicesArray.filter(device => { return device.deviceSKU === splittedItem[1]; }); if (deviceSKUavailabilityCheck.length === 0) { // @mani Forming the JSON object objToCreateJson['name'] = splittedItem[0]; objToCreateJson['deviceSKU'] = splittedItem[1]; objToCreateJson['isChecked'] = true; // @mani Pushing the selected item to the array this.selectedDevicesArray.forEach((device, index) => { if (device.isChecked === true) { this.previousSelectedIndex = index; } device.isChecked = false; }); if (splittedItem[0].indexOf('3-in-1') === -1){ this.selectedDevicesArray.push(objToCreateJson); } } else { this.selectedDevicesArray.forEach(device => { device.isChecked = (device.deviceSKU === splittedItem[1]) ? true : false; }); } // @mani Calling the function to filter accessories based on deviceSKU this.sortByDevice(splittedItem[1]); } /** * This function is used to select accessories based on previous selection * @param {} deviceSKU - deviceSKU of the selected device. * @param {} isChecked - Boolean value for checked status of the selected device. * @returns void */ accessoriesByPreviousSelections(deviceSKU, isChecked): void { const METHOD_NAME: string = 'accessoriesByPreviousSelections()'; // @mani Checking whether the checkbox is selected or not if (isChecked !== false) { // @mani If it is selected, make all the other checkbox to be unchecked this.selectedDevicesArray.forEach(device => { device.isChecked = (device.deviceSKU === deviceSKU) ? true : false; }); // @mani Calling the function to filter accessories based on deviceSKU this.sortByDevice(deviceSKU); } else { this.accessories = this.wholeAccessoryList; this.resetAccessoryFilters(); this.applyCustomSorting(); } } // @mani ==> Function call to remove the selected device from the device filter list /** * This function is used to remove the device from the array of selected devices. * @param {} deviceSKU- deviceSKU of the selected device. * @param {} isChecked- Boolean value for checked status of the selected device. * @returns void */ removeFromDeviceSelected(deviceSKU, isChecked): void { const METHOD_NAME: string = 'removeFromDeviceSelected()'; let deviceSKUIndex: number; this.selectedDevicesArray.forEach((selected, index) => { if (selected.deviceSKU === deviceSKU) { deviceSKUIndex = index; } }); if (this.selectedDevicesArray.length > 1) { this.selectedDevicesArray.splice(deviceSKUIndex, 1); this.sortByDevice(this.selectedDevicesArray[this.selectedDevicesArray.length - 1].deviceSKU); this.selectedDevicesArray[this.selectedDevicesArray.length - 1].isChecked = true; } else { this.selectedDevicesArray.splice(deviceSKUIndex, 1); this.accessories = this.wholeAccessoryList; this.applyCustomSorting(); } this.resetAccessoryFilters(); } /** * This function helps to get the suggested items from filters. * @returns void */ getSuggestionItems(): void { if (this.searchText && this.searchText.length > 0) { this.accSuggestions = this.marketingService.getAccessorySuggestions(this.storeId); if (this.accSuggestions && this.accSuggestions.length > 0) { this.suggestionItems = this.accSuggestions.filter((item) => { return item.toUpperCase().indexOf(this.searchText.toUpperCase(), 0) > -1; }); } } else { this.suggestionItems = []; } } /** * This function helps to select the suggestion items and calls applyCustomSorting function * @param {string} item - search string * @returns void */ selectSuggestionItem(item: string): void { this.searchText = item; this.suggestionItems = []; // this.applyCustomSorting(); if (this.searchText && this.searchText.length > 0) { this.applyCustomSorting(); } } updateFilter(accessorySelected:any , selectedAcc:any){ // if (accessorySelected !== false) { this.filters.forEach(filter => { filter.items.forEach(acc => { if(acc.name === selectedAcc){ acc.isChecked = accessorySelected; } // else{ // acc.isChecked = false; // } }); }); // } } sendCompareModalInfo(show: string){ this.compareService.compareProducts = this.compareProducts; this.compareService.leaseCRP = this.leaseCrp; this.compareService.loanCRP = this.loanCrp; this.compareService.deviceFeatureFilters = this.deviceFeatureFilters; this.compareService.setMessage(show); } sendMoreDetailsModalInfo(show: string, productItem: any){ this.moreDetailsService.product = productItem; this.moreDetailsService.setMessage(show); } }