import { ChangeDetectionStrategy , ChangeDetectorRef , Component , OnInit , Output , EventEmitter , OnDestroy , } from '@angular/core' ; import { FormGroup , FormControl } from '@angular/forms' ; import { Session } from '../../../../services/session' ; import { DialogService } from '../../../../common/services/confirm-leave-dialog.service' ; import { Observable , Subscription } from 'rxjs' ; import { MindsUser } from '../../../../interfaces/entities' ; import { SettingsV2Service } from '../../settings-v2.service' ; @ Component ( { selector : 'm-settingsV2__toasterNotifications' , templateUrl : './toaster-notifications.component.html' , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class SettingsV2ToasterNotificationsComponent implements OnInit , OnDestroy { @ Output ( ) formSubmitted : EventEmitter < any > = new EventEmitter ( ) ; init : boolean = false ; inProgress : boolean = false ; user : MindsUser ; settingsSubscription : Subscription ; form ; constructor ( protected cd , private session , protected settingsService , private dialogService ) { } ngOnInit ( ) { this . user = this . session . getLoggedInUser ( ) ; this . form = new FormGroup ( { toasterNotifications : new FormControl ( '' ) , } ) ; this . settingsSubscription = this . settingsService . settings$ . subscribe ( ( settings ) => { this . toasterNotifications . setValue ( settings . toaster_notifications ) ; this . detectChanges ( ) ; } ) ; this . init = true ; this . detectChanges ( ) ; } async submit ( ) { if ( ! this . canSubmit ( ) ) { return ; } try { this . inProgress = true ; this . detectChanges ( ) ; const formValue = { toaster_notifications : this . toasterNotifications . value , } ; const response = await this . settingsService . updateSettings ( this . user . guid , formValue ) ; if ( response . status === 'success' ) { this . formSubmitted . emit ( { formSubmitted : true } ) ; this . form . markAsPristine ( ) ; } } catch ( e ) { this . formSubmitted . emit ( { formSubmitted : false , error : e } ) ; } finally { this . inProgress = false ; this . detectChanges ( ) ; } } canSubmit ( ) { return this . form . valid && ! this . inProgress && ! this . form . pristine ; } detectChanges ( ) { this . cd . markForCheck ( ) ; this . cd . detectChanges ( ) ; } ngOnDestroy ( ) { if ( this . settingsSubscription ) { this . settingsSubscription . unsubscribe ( ) ; } } get toasterNotifications ( ) { return this . form . get ( 'toasterNotifications' ) ; } }