]>
Commit | Line | Data |
---|---|---|
c30745f3 | 1 | import { Component, Input, OnInit } from '@angular/core' |
df98563e | 2 | import { FormBuilder, FormGroup } from '@angular/forms' |
df98563e | 3 | import { NotificationsService } from 'angular2-notifications' |
c30745f3 C |
4 | import { UserUpdateMe } from '../../../../../../shared' |
5 | import { AuthService } from '../../../core' | |
6 | import { FormReactive, User, UserService } from '../../../shared' | |
af5e743b C |
7 | |
8 | @Component({ | |
9 | selector: 'my-account-details', | |
c30745f3 C |
10 | templateUrl: './account-details.component.html', |
11 | styleUrls: [ './account-details.component.scss' ] | |
af5e743b C |
12 | }) |
13 | ||
14 | export class AccountDetailsComponent extends FormReactive implements OnInit { | |
df98563e | 15 | @Input() user: User = null |
af5e743b | 16 | |
df98563e | 17 | error: string = null |
af5e743b | 18 | |
df98563e C |
19 | form: FormGroup |
20 | formErrors = {} | |
21 | validationMessages = {} | |
af5e743b | 22 | |
df98563e | 23 | constructor ( |
af5e743b C |
24 | private authService: AuthService, |
25 | private formBuilder: FormBuilder, | |
af5e743b C |
26 | private notificationsService: NotificationsService, |
27 | private userService: UserService | |
28 | ) { | |
df98563e | 29 | super() |
af5e743b C |
30 | } |
31 | ||
df98563e | 32 | buildForm () { |
af5e743b | 33 | this.form = this.formBuilder.group({ |
df98563e C |
34 | displayNSFW: [ this.user.displayNSFW ] |
35 | }) | |
af5e743b | 36 | |
df98563e | 37 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
af5e743b C |
38 | } |
39 | ||
df98563e C |
40 | ngOnInit () { |
41 | this.buildForm() | |
af5e743b C |
42 | } |
43 | ||
df98563e C |
44 | updateDetails () { |
45 | const displayNSFW = this.form.value['displayNSFW'] | |
8094a898 | 46 | const details: UserUpdateMe = { |
af5e743b | 47 | displayNSFW |
df98563e | 48 | } |
af5e743b | 49 | |
df98563e | 50 | this.error = null |
8094a898 | 51 | this.userService.updateMyDetails(details).subscribe( |
af5e743b | 52 | () => { |
8094a898 | 53 | this.notificationsService.success('Success', 'Information updated.') |
af5e743b | 54 | |
d592e0a9 | 55 | this.authService.refreshUserInformation() |
af5e743b C |
56 | }, |
57 | ||
f7354483 | 58 | err => this.error = err.message |
df98563e | 59 | ) |
af5e743b C |
60 | } |
61 | } |