1 import { Component, Input, OnInit } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
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 { forkJoin, Subject } from 'rxjs'
9 import { SelectItem } from 'primeng/api'
10 import { first } from 'rxjs/operators'
13 selector: 'my-account-video-settings',
14 templateUrl: './my-account-video-settings.component.html',
15 styleUrls: [ './my-account-video-settings.component.scss' ]
17 export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit {
18 @Input() user: User = null
19 @Input() userInformationLoaded: Subject<any>
21 languageItems: SelectItem[] = []
24 protected formValidatorService: FormValidatorService,
25 private authService: AuthService,
26 private notifier: Notifier,
27 private userService: UserService,
28 private serverService: ServerService,
37 webTorrentEnabled: null,
43 this.serverService.videoLanguagesLoaded.pipe(first()),
44 this.userInformationLoaded.pipe(first())
46 const languages = this.serverService.getVideoLanguages()
48 this.languageItems = [ { label: this.i18n('Unknown language'), value: '_unknown' } ]
49 this.languageItems = this.languageItems
50 .concat(languages.map(l => ({ label: l.label, value: l.id })))
52 const videoLanguages = this.user.videoLanguages
53 ? this.user.videoLanguages
54 : this.languageItems.map(l => l.value)
56 this.form.patchValue({
57 nsfwPolicy: this.user.nsfwPolicy,
58 webTorrentEnabled: this.user.webTorrentEnabled,
59 autoPlayVideo: this.user.autoPlayVideo === true,
66 const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
67 const webTorrentEnabled = this.form.value['webTorrentEnabled']
68 const autoPlayVideo = this.form.value['autoPlayVideo']
70 let videoLanguages: string[] = this.form.value['videoLanguages']
71 if (Array.isArray(videoLanguages)) {
72 if (videoLanguages.length === this.languageItems.length) {
73 videoLanguages = null // null means "All"
74 } else if (videoLanguages.length > 20) {
75 this.notifier.error('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.')
77 } else if (videoLanguages.length === 0) {
78 this.notifier.error('You need to enabled at least 1 video language.')
83 const details: UserUpdateMe = {
90 this.userService.updateMyProfile(details).subscribe(
92 this.notifier.success(this.i18n('Video settings updated.'))
94 this.authService.refreshUserInformation()
97 err => this.notifier.error(err.message)
101 getDefaultVideoLanguageLabel () {
102 return this.i18n('No language')
105 getSelectedVideoLanguageLabel () {
106 return this.i18n('{{\'{0} languages selected')