import { Component , OnInit , ViewContainerRef , OnDestroy } from '@angular/core' ; import { TdDialogService , LoadingMode , LoadingType , TdLoadingService , } from "@covalent/core" ; import { PrinterConfig , PrinterConfigService , RetailShop , RetailShopsService } from "../../../services/shop.service" ; import { Subscription } from "rxjs" ; import { MdSnackBar } from "@angular/material" ; import { IndexDBServiceService } from "../../../services/indexdb.service" ; @ Component ( { selector : 'app-printer-config' , templateUrl : './printer-config.component.html' , styleUrls : [ './printer-config.component.scss' ] , viewProviders : [ PrinterConfigService ] } ) export class PrinterConfigComponent implements OnInit , OnDestroy { shop : RetailShop ; shopSub : Subscription ; printerConfig : PrinterConfig ; constructor ( private _shopService , private _dialogService , private _indexDB , private _loadingService , private _snackBarService , private _printerService , private _viewContainerRef ) { this . _loadingService . create ( { name : 'printConfig' , type : LoadingType . Circular , mode : LoadingMode . Indeterminate , color : 'warn' , } ) ; } ngOnInit ( ) { this . shop = this . _shopService . shop ; if ( this . shop . id ) { this . copyPrinterConfig ( ) ; } this . shopSub = this . _shopService . shop$ . subscribe ( ( data ) => { this . shop = data ; this . copyPrinterConfig ( ) ; } ) ; if ( ! this . shop . id ) { this . _dialogService . openAlert ( { message : 'Select a Shop to edit Printer Config.' , disableClose : false , viewContainerRef : this . _viewContainerRef , title : 'Alert' , closeButton : 'Close' , } ) ; } } ngOnDestroy ( ) { this . shopSub . unsubscribe ( ) ; } save ( event ) { } copyPrinterConfig ( ) { this . printerConfig = Object . assign ( { } , this . shop . printer_config ) ; } cancelState ( ) { this . copyPrinterConfig ( ) ; } saveState ( ) { this . _loadingService . register ( 'printConfig' ) ; if ( this . printerConfig . id ) { this . _printerService . update ( this . printerConfig . id , this . printerConfig ) . subscribe ( ( data ) => { this . shop . printer_config = this . printerConfig ; this . _indexDB . shops . update ( this . shop . id , this . shop ) . then ( ( ) => { this . _shopService . shop = this . shop ; this . _loadingService . resolve ( 'printConfig' ) ; this . _snackBarService . open ( 'Printer Config Saved SuccessFully' , '' , { duration : 3000 } ) ; } ) ; } , ( ) => { this . _loadingService . resolve ( 'printConfig' ) ; } ) ; } else { this . _printerService . create ( this . printerConfig ) . subscribe ( ( data ) => { this . shop . printer_config = this . printerConfig ; this . _indexDB . shops . update ( this . shop . id , this . shop ) . then ( ( ) => { this . _shopService . shop = this . shop ; this . _loadingService . resolve ( 'printConfig' ) ; this . _snackBarService . open ( 'Printer Config Saved SuccessFully' , '' , { duration : 3000 } ) ; } ) ; } , ( ) => { this . _loadingService . resolve ( 'printConfig' ) ; } ) ; } } }