aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/+my-account/my-account-settings/my-account-profile/my-account-profile.component.ts
blob: 1fe337da0d51fcd87c103fc16204d44a25c6861a (plain) (tree)
1
2
3
4
5
6
7
                                                        


                                                                                                
                                                   

                                                                                               







                                                                               
                                              


                      
               
                                                         
                                                       

                                     



           



                                        

      





                                                      



                                                       
                                                              







                                                                             
                                                                                              





                                     
import { Component, Input, OnInit } from '@angular/core'
import { NotificationsService } from 'angular2-notifications'
import { FormReactive, USER_DESCRIPTION, USER_DISPLAY_NAME, UserService } from '../../../shared'
import { User } from '@app/shared'
import { I18n } from '@ngx-translate/i18n-polyfill'
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
import { Subject } from 'rxjs/Subject'

@Component({
  selector: 'my-account-profile',
  templateUrl: './my-account-profile.component.html',
  styleUrls: [ './my-account-profile.component.scss' ]
})
export class MyAccountProfileComponent extends FormReactive implements OnInit {
  @Input() user: User = null
  @Input() userInformationLoaded: Subject<any>

  error: string = null

  constructor (
    protected formValidatorService: FormValidatorService,
    private notificationsService: NotificationsService,
    private userService: UserService,
    private i18n: I18n
  ) {
    super()
  }

  ngOnInit () {
    this.buildForm({
      'display-name': USER_DISPLAY_NAME,
      description: USER_DESCRIPTION
    })

    this.userInformationLoaded.subscribe(() => {
      this.form.patchValue({
        'display-name': this.user.account.displayName,
        description: this.user.account.description
      })
    })
  }

  updateMyProfile () {
    const displayName = this.form.value['display-name']
    const description = this.form.value['description'] || null

    this.error = null

    this.userService.updateMyProfile({ displayName, description }).subscribe(
      () => {
        this.user.account.displayName = displayName
        this.user.account.description = description

        this.notificationsService.success(this.i18n('Success'), this.i18n('Profile updated.'))
      },

      err => this.error = err.message
    )
  }
}