import { Component , OnInit } from '@angular/core' ; import { FormBuilder , FormGroup } from '@angular/forms' ; import { Router } from '@angular/router' ; import { User , UserService } from '../core' ; @ Component ( { selector : 'app-settings-page' , templateUrl : './settings.component.html' } ) export class SettingsComponent implements OnInit { user : User = { } as User ; settingsForm : FormGroup ; errors : Object = { } ; isSubmitting = false ; constructor ( private router , private userService , private fb ) { this . settingsForm = this . fb . group ( { image : '' , username : '' , bio : '' , email : '' , password : '' } ) ; } ngOnInit ( ) { Object . assign ( this . user , this . userService . getCurrentUser ( ) ) ; this . settingsForm . patchValue ( this . user ) ; } logout ( ) { this . userService . purgeAuth ( ) ; this . router . navigateByUrl ( '/' ) ; } submitForm ( ) { this . isSubmitting = true ; this . updateUser ( this . settingsForm . value ) ; this . userService . update ( this . user ) . subscribe ( updatedUser => this . router . navigateByUrl ( '/profile/' + updatedUser . username ) , err => { this . errors = err ; this . isSubmitting = false ; } ) ; } updateUser ( values ) { Object . assign ( this . user , values ) ; } }