-import { Component, Input, OnInit } from '@angular/core'
-import { FormBuilder, FormGroup } from '@angular/forms'
-import { NotificationsService } from 'angular2-notifications'
-import { UserUpdateMe } from '../../../../../../shared'
+import { Component, Input, OnInit, OnDestroy } from '@angular/core'
+import { Notifier, ServerService } from '@app/core'
+import { UserUpdateMe } from '../../../../../../shared/models/users'
+import { User, UserService } from '@app/shared/users'
import { AuthService } from '../../../core'
-import { FormReactive, User, UserService } from '../../../shared'
+import { FormReactive } from '@app/shared/forms/form-reactive'
+import { I18n } from '@ngx-translate/i18n-polyfill'
+import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
+import { forkJoin, Subject, Subscription } from 'rxjs'
+import { SelectItem } from 'primeng/api'
+import { first } from 'rxjs/operators'
+import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
+import { pick } from 'lodash-es'
@Component({
selector: 'my-account-video-settings',
templateUrl: './my-account-video-settings.component.html',
styleUrls: [ './my-account-video-settings.component.scss' ]
})
-export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit {
+export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit, OnDestroy {
@Input() user: User = null
+ @Input() reactiveUpdate = false
+ @Input() notifyOnUpdate = true
+ @Input() userInformationLoaded: Subject<any>
- form: FormGroup
- formErrors = {}
- validationMessages = {}
+ languageItems: SelectItem[] = []
+ defaultNSFWPolicy: NSFWPolicyType
+ formValuesWatcher: Subscription
constructor (
+ protected formValidatorService: FormValidatorService,
private authService: AuthService,
- private formBuilder: FormBuilder,
- private notificationsService: NotificationsService,
- private userService: UserService
+ private notifier: Notifier,
+ private userService: UserService,
+ private serverService: ServerService,
+ private i18n: I18n
) {
super()
}
- buildForm () {
- this.form = this.formBuilder.group({
- nsfwPolicy: [ this.user.nsfwPolicy ],
- autoPlayVideo: [ this.user.autoPlayVideo ]
+ ngOnInit () {
+ let oldForm: any
+
+ this.buildForm({
+ nsfwPolicy: null,
+ webTorrentEnabled: null,
+ autoPlayVideo: null,
+ autoPlayNextVideo: null,
+ videoLanguages: null
})
- this.form.valueChanges.subscribe(data => this.onValueChanged(data))
+ forkJoin([
+ this.serverService.getVideoLanguages(),
+ this.serverService.getConfig(),
+ this.userInformationLoaded.pipe(first())
+ ]).subscribe(([ languages, config ]) => {
+ this.languageItems = [ { label: this.i18n('Unknown language'), value: '_unknown' } ]
+ this.languageItems = this.languageItems
+ .concat(languages.map(l => ({ label: l.label, value: l.id })))
+
+ const videoLanguages = this.user.videoLanguages
+ ? this.user.videoLanguages
+ : this.languageItems.map(l => l.value)
+
+ this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy
+
+ this.form.patchValue({
+ nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy,
+ webTorrentEnabled: this.user.webTorrentEnabled,
+ autoPlayVideo: this.user.autoPlayVideo === true,
+ autoPlayNextVideo: this.user.autoPlayNextVideo,
+ videoLanguages
+ })
+
+ if (this.reactiveUpdate) {
+ oldForm = { ...this.form.value }
+ this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => {
+ const updatedKey = Object.keys(formValue).find(k => formValue[k] !== oldForm[k])
+ oldForm = { ...this.form.value }
+ this.updateDetails([updatedKey])
+ })
+ }
+ })
}
- ngOnInit () {
- this.buildForm()
+ ngOnDestroy () {
+ this.formValuesWatcher?.unsubscribe()
}
- updateDetails () {
- const nsfwPolicy = this.form.value['nsfwPolicy']
+ updateDetails (onlyKeys?: string[]) {
+ const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
+ const webTorrentEnabled = this.form.value['webTorrentEnabled']
const autoPlayVideo = this.form.value['autoPlayVideo']
- const details: UserUpdateMe = {
+ const autoPlayNextVideo = this.form.value['autoPlayNextVideo']
+
+ let videoLanguages: string[] = this.form.value['videoLanguages']
+ if (Array.isArray(videoLanguages)) {
+ if (videoLanguages.length === this.languageItems.length) {
+ videoLanguages = null // null means "All"
+ } else if (videoLanguages.length > 20) {
+ this.notifier.error('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.')
+ return
+ } else if (videoLanguages.length === 0) {
+ this.notifier.error('You need to enabled at least 1 video language.')
+ return
+ }
+ }
+
+ let details: UserUpdateMe = {
nsfwPolicy,
- autoPlayVideo
+ webTorrentEnabled,
+ autoPlayVideo,
+ autoPlayNextVideo,
+ videoLanguages
}
- this.userService.updateMyProfile(details).subscribe(
- () => {
- this.notificationsService.success('Success', 'Information updated.')
+ if (onlyKeys) details = pick(details, onlyKeys)
+
+ if (this.authService.isLoggedIn()) {
+ this.userService.updateMyProfile(details).subscribe(
+ () => {
+ this.authService.refreshUserInformation()
+
+ if (this.notifyOnUpdate) this.notifier.success(this.i18n('Video settings updated.'))
+ },
- this.authService.refreshUserInformation()
- },
+ err => this.notifier.error(err.message)
+ )
+ } else {
+ this.userService.updateMyAnonymousProfile(details)
+ if (this.notifyOnUpdate) this.notifier.success(this.i18n('Display/Video settings updated.'))
+ }
+ }
+
+ getDefaultVideoLanguageLabel () {
+ return this.i18n('No language')
+ }
- err => this.notificationsService.error('Error', err.message)
- )
+ getSelectedVideoLanguageLabel () {
+ return this.i18n('{{\'{0} languages selected')
}
}