]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-user-settings/user-video-settings.component.ts
Live views update
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-settings / user-video-settings.component.ts
index 4e4539936f93d17d07014bec2eefb8738a4a1df6..2497e001c8872944ff87a2bd6746709aeef0bdf7 100644 (file)
@@ -1,11 +1,9 @@
 import { pick } from 'lodash-es'
-import { SelectItem } from 'primeng/api'
 import { forkJoin, Subject, Subscription } from 'rxjs'
 import { first } from 'rxjs/operators'
 import { Component, Input, OnDestroy, OnInit } from '@angular/core'
 import { AuthService, Notifier, ServerService, User, UserService } from '@app/core'
-import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
-import { I18n } from '@ngx-translate/i18n-polyfill'
+import { FormReactive, FormValidatorService, ItemSelectCheckboxValue, SelectOptionsItem } from '@app/shared/shared-forms'
 import { UserUpdateMe } from '@shared/models'
 import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
 
@@ -20,22 +18,25 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
   @Input() notifyOnUpdate = true
   @Input() userInformationLoaded: Subject<any>
 
-  languageItems: SelectItem[] = []
+  languageItems: SelectOptionsItem[] = []
   defaultNSFWPolicy: NSFWPolicyType
   formValuesWatcher: Subscription
 
+  private allLanguagesGroup: string
+
   constructor (
     protected formValidatorService: FormValidatorService,
     private authService: AuthService,
     private notifier: Notifier,
     private userService: UserService,
-    private serverService: ServerService,
-    private i18n: I18n
+    private serverService: ServerService
   ) {
     super()
   }
 
   ngOnInit () {
+    this.allLanguagesGroup = $localize`All languages`
+
     let oldForm: any
 
     this.buildForm({
@@ -51,13 +52,15 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
       this.serverService.getConfig(),
       this.userInformationLoaded.pipe(first())
     ]).subscribe(([ languages, config ]) => {
-      this.languageItems = [ { label: this.i18n('Unknown language'), value: '_unknown' } ]
+      const group = this.allLanguagesGroup
+
+      this.languageItems = [ { label: $localize`Unknown language`, id: '_unknown', group } ]
       this.languageItems = this.languageItems
-                               .concat(languages.map(l => ({ label: l.label, value: l.id })))
+                               .concat(languages.map(l => ({ label: l.label, id: l.id, group })))
 
-      const videoLanguages = this.user.videoLanguages
-        ? this.user.videoLanguages
-        : this.languageItems.map(l => l.value)
+      const videoLanguages: ItemSelectCheckboxValue[] = this.user.videoLanguages
+        ? this.user.videoLanguages.map(l => ({ id: l }))
+        : [ { group } ]
 
       this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy
 
@@ -71,10 +74,12 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
 
       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])
+
+          this.updateDetails([ updatedKey ])
         })
       }
     })
@@ -90,19 +95,22 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
     const autoPlayVideo = this.form.value['autoPlayVideo']
     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.')
+    const videoLanguagesForm = this.form.value['videoLanguages']
+
+    if (Array.isArray(videoLanguagesForm)) {
+      if (videoLanguagesForm.length > 20) {
+        this.notifier.error($localize`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.')
+      }
+
+      if (videoLanguagesForm.length === 0) {
+        this.notifier.error($localize`You need to enable at least 1 video language.`)
         return
       }
     }
 
+    const videoLanguages = this.getVideoLanguages(videoLanguagesForm)
+
     let details: UserUpdateMe = {
       nsfwPolicy,
       webTorrentEnabled,
@@ -111,6 +119,10 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
       videoLanguages
     }
 
+    if (videoLanguages) {
+      details = Object.assign(details, videoLanguages)
+    }
+
     if (onlyKeys) details = pick(details, onlyKeys)
 
     if (this.authService.isLoggedIn()) {
@@ -118,22 +130,39 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
         () => {
           this.authService.refreshUserInformation()
 
-          if (this.notifyOnUpdate) this.notifier.success(this.i18n('Video settings updated.'))
+          if (this.notifyOnUpdate) this.notifier.success($localize`Video settings updated.`)
         },
 
         err => this.notifier.error(err.message)
       )
     } else {
       this.userService.updateMyAnonymousProfile(details)
-      if (this.notifyOnUpdate) this.notifier.success(this.i18n('Display/Video settings updated.'))
+      if (this.notifyOnUpdate) this.notifier.success($localize`Display/Video settings updated.`)
     }
   }
 
-  getDefaultVideoLanguageLabel () {
-    return this.i18n('No language')
-  }
+  private getVideoLanguages (videoLanguages: ItemSelectCheckboxValue[]) {
+    if (!Array.isArray(videoLanguages)) return undefined
+
+    // null means "All"
+    if (videoLanguages.length === this.languageItems.length) return null
+
+    if (videoLanguages.length === 1) {
+      const videoLanguage = videoLanguages[0]
+
+      if (typeof videoLanguage === 'string') {
+        if (videoLanguage === this.allLanguagesGroup) return null
+      } else {
+        if (videoLanguage.group === this.allLanguagesGroup) return null
+      }
+    }
+
+    return videoLanguages.map(l => {
+      if (typeof l === 'string') return l
 
-  getSelectedVideoLanguageLabel () {
-    return this.i18n('{{\'{0} languages selected')
+      if (l.group) return l.group
+
+      return l.id + ''
+    })
   }
 }