85b3a48cc728b60fb411d1913af764c345bb6717
[oweals/peertube.git] /
1 import { Component, Input, OnInit } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { UserUpdateMe } from '../../../../../../shared'
4 import { AuthService } from '../../../core'
5 import { FormReactive, User, UserService } from '../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8 import { Subject } from 'rxjs/Subject'
9
10 @Component({
11   selector: 'my-account-video-settings',
12   templateUrl: './my-account-video-settings.component.html',
13   styleUrls: [ './my-account-video-settings.component.scss' ]
14 })
15 export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit {
16   @Input() user: User = null
17   @Input() userInformationLoaded: Subject<any>
18
19   constructor (
20     protected formValidatorService: FormValidatorService,
21     private authService: AuthService,
22     private notificationsService: NotificationsService,
23     private userService: UserService,
24     private i18n: I18n
25   ) {
26     super()
27   }
28
29   ngOnInit () {
30     this.buildForm({
31       nsfwPolicy: null,
32       autoPlayVideo: null
33     })
34
35     this.userInformationLoaded.subscribe(() => {
36       this.form.patchValue({
37         nsfwPolicy: this.user.nsfwPolicy,
38         autoPlayVideo: this.user.autoPlayVideo === true ? 'true' : 'false'
39       })
40     })
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.updateMyProfile(details).subscribe(
52       () => {
53         this.notificationsService.success(this.i18n('Success'), this.i18n('Information updated.'))
54
55         this.authService.refreshUserInformation()
56       },
57
58       err => this.notificationsService.error(this.i18n('Error'), err.message)
59     )
60   }
61 }