aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/account/account-settings/account-details/account-details.component.ts
blob: b8c19d8d608774f348529413dc07003ec96138a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Component, Input, OnInit } from '@angular/core'
import { FormBuilder, FormGroup } from '@angular/forms'
import { NotificationsService } from 'angular2-notifications'
import { UserUpdateMe } from '../../../../../../shared'
import { AuthService } from '../../../core'
import { FormReactive, User, UserService } from '../../../shared'

@Component({
  selector: 'my-account-details',
  templateUrl: './account-details.component.html',
  styleUrls: [ './account-details.component.scss' ]
})

export class AccountDetailsComponent extends FormReactive implements OnInit {
  @Input() user: User = null

  error: string = null

  form: FormGroup
  formErrors = {}
  validationMessages = {}

  constructor (
    private authService: AuthService,
    private formBuilder: FormBuilder,
    private notificationsService: NotificationsService,
    private userService: UserService
  ) {
    super()
  }

  buildForm () {
    this.form = this.formBuilder.group({
      displayNSFW: [ this.user.displayNSFW ],
      autoPlayVideo: [ this.user.autoPlayVideo ]
    })

    this.form.valueChanges.subscribe(data => this.onValueChanged(data))
  }

  ngOnInit () {
    this.buildForm()
  }

  updateDetails () {
    const displayNSFW = this.form.value['displayNSFW']
    const autoPlayVideo = this.form.value['autoPlayVideo']
    const details: UserUpdateMe = {
      displayNSFW,
      autoPlayVideo
    }

    this.error = null
    this.userService.updateMyDetails(details).subscribe(
      () => {
        this.notificationsService.success('Success', 'Information updated.')

        this.authService.refreshUserInformation()
      },

      err => this.error = err.message
    )
  }
}