import { Component , OnDestroy , OnInit } from '@angular/core' ; import { Subscription } from 'rxjs' ; import { CmdbType } from '../../models/cmdb-type' ; import { CategoryService } from '../../services/category.service' ; import { TypeService } from '../../services/type.service' ; import { ActivatedRoute , Params , Router } from '@angular/router' ; import { CmdbCategory } from '../../models/cmdb-category' ; import { CmdbMode } from '../../modes.enum' ; import { SidebarService } from '../../../layout/services/sidebar.service' ; import { APIGetMultiResponse } from '../../../services/models/api-response' ; @ Component ( { selector : 'cmdb-category-edit' , templateUrl : './category-edit.component.html' , styleUrls : [ './category-edit.component.scss' ] } ) export class CategoryEditComponent implements OnInit , OnDestroy { public formValid : boolean = false ; public publicID : number ; public mode : CmdbMode = CmdbMode . Edit ; private categoryServiceSubscription : Subscription = new Subscription ( ) ; private typeUnAssignedSubscription : Subscription = new Subscription ( ) ; private typeAssignedSubscription : Subscription = new Subscription ( ) ; private categorySubmitSubscription : Subscription = new Subscription ( ) ; public category : CmdbCategory ; public unAssignedTypes : CmdbType [ ] = [ ] ; public assignedTypes : CmdbType [ ] = [ ] ; constructor ( private categoryService , private typeService , private router , private route , private sidebarService ) { this . route . params . subscribe ( ( params ) => { this . publicID = Number ( params . publicID ) ; } ) ; } public ngOnInit ( ) { this . categoryServiceSubscription = this . categoryService . getCategory ( this . publicID ) . subscribe ( ( category ) => { this . category = category ; } ) ; this . typeAssignedSubscription = this . typeService . getTypeListByCategory ( this . publicID ) . subscribe ( ( types ) => { this . assignedTypes = types ; } ) ; this . typeUnAssignedSubscription = this . typeService . getUncategorizedTypes ( ) . subscribe ( ( apiResponse ) => { this . unAssignedTypes = apiResponse . results ; } ) ; } public ngOnDestroy ( ) { this . categoryServiceSubscription . unsubscribe ( ) ; this . typeUnAssignedSubscription . unsubscribe ( ) ; this . typeAssignedSubscription . unsubscribe ( ) ; this . categorySubmitSubscription . unsubscribe ( ) ; } public onSave ( category ) { if ( this . formValid ) { this . categorySubmitSubscription = this . categoryService . updateCategory ( category ) . subscribe ( ( ) => { this . sidebarService . loadCategoryTree ( ) ; this . router . navigate ( [ '/' , 'framework' , 'category' ] ) ; } ) ; } } }