]>
Commit | Line | Data |
---|---|---|
ed56ad11 C |
1 | import { Component, Input, OnInit } from '@angular/core' |
2 | import { FormBuilder, FormGroup } from '@angular/forms' | |
3 | import { NotificationsService } from 'angular2-notifications' | |
4 | import { FormReactive, USER_DESCRIPTION, USER_DISPLAY_NAME, UserService } from '../../../shared' | |
5 | import { User } from '@app/shared' | |
b1d40cff | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
ed56ad11 C |
7 | |
8 | @Component({ | |
9 | selector: 'my-account-profile', | |
10 | templateUrl: './my-account-profile.component.html', | |
11 | styleUrls: [ './my-account-profile.component.scss' ] | |
12 | }) | |
13 | export class MyAccountProfileComponent extends FormReactive implements OnInit { | |
14 | @Input() user: User = null | |
15 | ||
16 | error: string = null | |
17 | ||
18 | form: FormGroup | |
19 | formErrors = { | |
20 | 'display-name': '', | |
21 | 'description': '' | |
22 | } | |
23 | validationMessages = { | |
24 | 'display-name': USER_DISPLAY_NAME.MESSAGES, | |
25 | 'description': USER_DESCRIPTION.MESSAGES | |
26 | } | |
27 | ||
28 | constructor ( | |
29 | private formBuilder: FormBuilder, | |
30 | private notificationsService: NotificationsService, | |
b1d40cff C |
31 | private userService: UserService, |
32 | private i18n: I18n | |
ed56ad11 C |
33 | ) { |
34 | super() | |
35 | } | |
36 | ||
37 | buildForm () { | |
38 | this.form = this.formBuilder.group({ | |
39 | 'display-name': [ this.user.account.displayName, USER_DISPLAY_NAME.VALIDATORS ], | |
40 | 'description': [ this.user.account.description, USER_DESCRIPTION.VALIDATORS ] | |
41 | }) | |
42 | ||
43 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
44 | } | |
45 | ||
46 | ngOnInit () { | |
47 | this.buildForm() | |
48 | } | |
49 | ||
50 | updateMyProfile () { | |
51 | const displayName = this.form.value['display-name'] | |
360329cc | 52 | const description = this.form.value['description'] || null |
ed56ad11 C |
53 | |
54 | this.error = null | |
55 | ||
56 | this.userService.updateMyProfile({ displayName, description }).subscribe( | |
57 | () => { | |
58 | this.user.account.displayName = displayName | |
59 | this.user.account.description = description | |
60 | ||
b1d40cff | 61 | this.notificationsService.success(this.i18n('Success'), this.i18n('Profile updated.')) |
ed56ad11 C |
62 | }, |
63 | ||
64 | err => this.error = err.message | |
65 | ) | |
66 | } | |
67 | } |