]>
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({ |
7efe153b AL |
34 | displayNSFW: [ this.user.displayNSFW ], |
35 | autoPlayVideo: [ this.user.autoPlayVideo ] | |
df98563e | 36 | }) |
af5e743b | 37 | |
df98563e | 38 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
af5e743b C |
39 | } |
40 | ||
df98563e C |
41 | ngOnInit () { |
42 | this.buildForm() | |
af5e743b C |
43 | } |
44 | ||
df98563e C |
45 | updateDetails () { |
46 | const displayNSFW = this.form.value['displayNSFW'] | |
7efe153b | 47 | const autoPlayVideo = this.form.value['autoPlayVideo'] |
8094a898 | 48 | const details: UserUpdateMe = { |
7efe153b AL |
49 | displayNSFW, |
50 | autoPlayVideo | |
df98563e | 51 | } |
af5e743b | 52 | |
df98563e | 53 | this.error = null |
8094a898 | 54 | this.userService.updateMyDetails(details).subscribe( |
af5e743b | 55 | () => { |
8094a898 | 56 | this.notificationsService.success('Success', 'Information updated.') |
af5e743b | 57 | |
d592e0a9 | 58 | this.authService.refreshUserInformation() |
af5e743b C |
59 | }, |
60 | ||
f7354483 | 61 | err => this.error = err.message |
df98563e | 62 | ) |
af5e743b C |
63 | } |
64 | } |