import { Component , OnInit , ChangeDetectionStrategy } from '@angular/core' ; import { AuthService } from '../auth.service' ; import { BehaviorSubject } from 'rxjs' ; import { FormGroup , FormControl } from '@angular/forms' ; @ Component ( { selector : 'app-private' , templateUrl : './private.component.html' , styleUrls : [ './private.component.css' ] , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class PrivateComponent implements OnInit { private userDetails_ : BehaviorSubject < any [ ] > = new BehaviorSubject ( undefined ) ; public userDetails = this . userDetails_ . asObservable ( ) ; public userDetailsForm = new FormGroup ( { } ) ; private busy_ = new BehaviorSubject ( false ) ; public busy = this . busy_ . asObservable ( ) ; private errorMessage_ = new BehaviorSubject ( '' ) ; public errorMessage = this . errorMessage_ . asObservable ( ) ; constructor ( private auth ) { } ngOnInit ( ) { this . getUserDetails ( ) ; } public async getUserDetails ( ) { this . busy_ . next ( true ) ; this . errorMessage_ . next ( '' ) ; try { const userDetails = await this . auth . getUserDetails ( ) ; userDetails . forEach ( detail => { const control = new FormControl ( detail . getValue ( ) ) ; this . userDetailsForm . addControl ( detail . getName ( ) , control ) ; } ) ; this . userDetails_ . next ( userDetails ) ; } catch ( err ) { this . errorMessage_ . next ( err . message || err ) ; } finally { this . busy_ . next ( false ) ; } } }