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