]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-settings/my-account-profile/my-account-profile.component.ts
Refactor form reactive
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-profile / my-account-profile.component.ts
CommitLineData
67ed6552 1import { Subject } from 'rxjs'
ed56ad11 2import { Component, Input, OnInit } from '@angular/core'
67ed6552 3import { Notifier, User, UserService } from '@app/core'
7ed1edbb 4import { USER_DESCRIPTION_VALIDATOR, USER_DISPLAY_NAME_REQUIRED_VALIDATOR } from '@app/shared/form-validators/user-validators'
5c5bcea2 5import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
ed56ad11
C
6
7@Component({
8 selector: 'my-account-profile',
9 templateUrl: './my-account-profile.component.html',
10 styleUrls: [ './my-account-profile.component.scss' ]
11})
12export class MyAccountProfileComponent extends FormReactive implements OnInit {
13 @Input() user: User = null
d18d6478 14 @Input() userInformationLoaded: Subject<any>
ed56ad11
C
15
16 error: string = null
17
ed56ad11 18 constructor (
5c5bcea2 19 protected formReactiveService: FormReactiveService,
f8b2c1b4 20 private notifier: Notifier,
66357162 21 private userService: UserService
7ed1edbb 22 ) {
ed56ad11
C
23 super()
24 }
25
d18d6478
C
26 ngOnInit () {
27 this.buildForm({
9dfaa38c 28 username: null,
7ed1edbb
C
29 'display-name': USER_DISPLAY_NAME_REQUIRED_VALIDATOR,
30 description: USER_DESCRIPTION_VALIDATOR
ed56ad11 31 })
9dfaa38c 32 this.form.controls['username'].disable()
ed56ad11 33
d18d6478
C
34 this.userInformationLoaded.subscribe(() => {
35 this.form.patchValue({
9dfaa38c 36 username: this.user.username,
d18d6478
C
37 'display-name': this.user.account.displayName,
38 description: this.user.account.description
39 })
40 })
ed56ad11
C
41 }
42
52a3f561
RK
43 get instanceHost () {
44 return window.location.host
45 }
46
ed56ad11
C
47 updateMyProfile () {
48 const displayName = this.form.value['display-name']
360329cc 49 const description = this.form.value['description'] || null
ed56ad11
C
50
51 this.error = null
52
1378c0d3
C
53 this.userService.updateMyProfile({ displayName, description })
54 .subscribe({
55 next: () => {
ed56ad11
C
56 this.user.account.displayName = displayName
57 this.user.account.description = description
58
66357162 59 this.notifier.success($localize`Profile updated.`)
ed56ad11
C
60 },
61
1378c0d3
C
62 error: err => this.error = err.message
63 })
ed56ad11
C
64 }
65}