]>
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 C |
17 | form: FormGroup |
18 | formErrors = {} | |
19 | validationMessages = {} | |
af5e743b | 20 | |
df98563e | 21 | constructor ( |
af5e743b C |
22 | private authService: AuthService, |
23 | private formBuilder: FormBuilder, | |
af5e743b C |
24 | private notificationsService: NotificationsService, |
25 | private userService: UserService | |
26 | ) { | |
df98563e | 27 | super() |
af5e743b C |
28 | } |
29 | ||
df98563e | 30 | buildForm () { |
af5e743b | 31 | this.form = this.formBuilder.group({ |
7efe153b AL |
32 | displayNSFW: [ this.user.displayNSFW ], |
33 | autoPlayVideo: [ this.user.autoPlayVideo ] | |
df98563e | 34 | }) |
af5e743b | 35 | |
df98563e | 36 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
af5e743b C |
37 | } |
38 | ||
df98563e C |
39 | ngOnInit () { |
40 | this.buildForm() | |
af5e743b C |
41 | } |
42 | ||
df98563e C |
43 | updateDetails () { |
44 | const displayNSFW = this.form.value['displayNSFW'] | |
7efe153b | 45 | const autoPlayVideo = this.form.value['autoPlayVideo'] |
8094a898 | 46 | const details: UserUpdateMe = { |
7efe153b AL |
47 | displayNSFW, |
48 | autoPlayVideo | |
df98563e | 49 | } |
af5e743b | 50 | |
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 | ||
fd206f0b | 58 | err => this.notificationsService.error('Error', err.message) |
df98563e | 59 | ) |
af5e743b C |
60 | } |
61 | } |