import { Component , OnInit } from '@angular/core' ; import { MdDialogRef } from "@angular/material" ; import { Tag , TagsService } from "../../../../services/items.service" ; import { TdLoadingService , LoadingType , LoadingMode } from "@covalent/core" ; import { RetailShopsService , RetailShop } from "../../../../services/shop.service" ; @ Component ( { selector : 'app-tag-form' , templateUrl : './tag-form.component.html' , styleUrls : [ './tag-form.component.scss' ] } ) export class TagFormComponent implements OnInit { tag : Tag ; tagCopy : Tag ; shops : RetailShop [ ] ; constructor ( public dialogRef , private _loadingService , private _shopService , private _tagService ) { this . _loadingService . create ( { name : 'tag-form' , type : LoadingType . Circular , mode : LoadingMode . Indeterminate , color : 'warn' , } ) ; } ngOnInit ( ) { this . shops = this . _shopService . shops ; this . _shopService . shops$ . subscribe ( ( data ) => { this . shops = data ; } ) ; this . tagCopy = Object . assign ( { } , this . tag ) ; } cancelState ( ) { this . tag = Object . assign ( { } , this . tagCopy ) ; } saveState ( ) { this . _loadingService . register ( 'tag-form' ) ; this . tag . retail_shop_id = this . tag . retail_shop . id ; if ( this . tag . id ) { this . _tagService . update ( this . tag . id , this . tag ) . subscribe ( ( ) => { this . dialogRef . close ( this . tag ) ; this . _loadingService . resolve ( 'tag-form' ) ; } , ( ) => { this . _loadingService . resolve ( 'tag-form' ) ; } ) ; } else { this . _tagService . create ( this . tag ) . subscribe ( ( data ) => { this . dialogRef . close ( data . data [ 0 ] ) ; this . _loadingService . resolve ( 'tag-form' ) ; } , ( ) => { this . _loadingService . resolve ( 'tag-form' ) ; } ) ; } } close ( ) { this . dialogRef . close ( ) ; } }