import { Component , Input , OnDestroy , OnInit } from '@angular/core' ; import { ActivatedRoute , Router } from '@angular/router' ; import { Subject , Subscription } from 'rxjs' ; import { CanComponentDeactivate , EntityService , ModalService , ToastService } from '../../core' ; import { Session } from '../shared/session.model' ; import { SessionService } from '../shared/session.service' ; @ Component ( { selector : 'ev-session' , templateUrl : 'session.component.html' , styleUrls : [ 'session.component.css' ] } ) export class SessionComponent implements OnDestroy , OnInit , CanComponentDeactivate { private subs = new Subscription ( ) ; @ Input ( ) session : Session ; editSession : Session = < Session > { } ; private id : any ; constructor ( private entityService , private modalService , private route , private router , private sessionService , private toastService ) { } cancel ( showToast = true ) { this . editSession = this . entityService . clone ( this . session ) ; if ( showToast ) { this . toastService . activate ( `Cancelled changes to ${ this . session . name }` ) ; } } canDeactivate ( ) { return ! this . session || ! this . isDirty ( ) || this . modalService . activate ( ) ; } delete ( ) { const msg = `Do you want to delete the ${ this . session . name }?` ; this . modalService . activate ( msg ) . then ( responseOK => { if ( responseOK ) { this . cancel ( false ) ; this . sessionService . deleteSession ( this . session ) . subscribe ( ( ) => { this . toastService . activate ( `Deleted ${ this . session . name }` ) ; this . gotoSessions ( ) ; } , err => this . handleServiceError ( 'Delete' , err ) , ( ) => console . log ( 'Delete Completed' ) ) ; } } ) ; } isAddMode ( ) { return isNaN ( this . id ) ; } ngOnDestroy ( ) { this . subs . unsubscribe ( ) ; } ngOnInit ( ) { componentHandler . upgradeDom ( ) ; this . subs . add ( this . sessionService . onDbReset . subscribe ( ( ) => this . getSession ( ) ) ) ; this . route . data . subscribe ( ( data ) => { this . setEditSession ( data . session ) ; this . id = this . session . id ; } ) ; } save ( ) { const session = ( this . session = this . entityService . merge ( this . session , this . editSession ) ) ; if ( session . id == null ) { this . sessionService . addSession ( session ) . subscribe ( s => { this . setEditSession ( s ) ; this . toastService . activate ( `Successfully added ${ s . name }` ) ; this . gotoSessions ( ) ; } ) ; return ; } this . sessionService . updateSession ( this . session ) . subscribe ( ( ) => this . toastService . activate ( `Successfully saved ${ this . session . name }` ) ) ; } private getSession ( ) { if ( this . id === 0 ) { return ; } if ( this . isAddMode ( ) ) { this . session = < Session > { name : '' , level : '' } ; this . editSession = this . entityService . clone ( this . session ) ; return ; } this . sessionService . getSession ( this . id ) . subscribe ( ( session ) => this . setEditSession ( session ) ) ; } private gotoSessions ( ) { this . router . navigate ( [ '/sessions' ] ) ; } private handleServiceError ( op , err ) { console . error ( `${ op } error: ${ err . message || err }` ) ; } private isDirty ( ) { return this . entityService . propertiesDiffer ( this . session , this . editSession ) ; } private setEditSession ( session ) { if ( session ) { this . session = session ; this . editSession = this . entityService . clone ( this . session ) ; } else { this . gotoSessions ( ) ; } } }