acc70c14d7f833c02d2ba32d96cece1cbe77ec9a
[oweals/peertube.git] /
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-video-settings',
10   templateUrl: './my-account-video-settings.component.html',
11   styleUrls: [ './my-account-video-settings.component.scss' ]
12 })
13 export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit {
14   @Input() user: User = null
15
16   form: FormGroup
17   formErrors = {}
18   validationMessages = {}
19
20   constructor (
21     private authService: AuthService,
22     private formBuilder: FormBuilder,
23     private notificationsService: NotificationsService,
24     private userService: UserService
25   ) {
26     super()
27   }
28
29   buildForm () {
30     this.form = this.formBuilder.group({
31       nsfwPolicy: [ this.user.nsfwPolicy ],
32       autoPlayVideo: [ this.user.autoPlayVideo ]
33     })
34
35     this.form.valueChanges.subscribe(data => this.onValueChanged(data))
36   }
37
38   ngOnInit () {
39     this.buildForm()
40   }
41
42   updateDetails () {
43     const nsfwPolicy = this.form.value['nsfwPolicy']
44     const autoPlayVideo = this.form.value['autoPlayVideo']
45     const details: UserUpdateMe = {
46       nsfwPolicy,
47       autoPlayVideo
48     }
49
50     this.userService.updateMyProfile(details).subscribe(
51       () => {
52         this.notificationsService.success('Success', 'Information updated.')
53
54         this.authService.refreshUserInformation()
55       },
56
57       err => this.notificationsService.error('Error', err.message)
58     )
59   }
60 }