]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.ts
Put box-shadow for input focus in variables, apply form-control to p-multiselect
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-video-settings / my-account-video-settings.component.ts
1 import { Component, Input, OnInit, OnDestroy } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { UserUpdateMe } from '../../../../../../shared/models/users'
4 import { User, UserService } from '@app/shared/users'
5 import { AuthService } from '../../../core'
6 import { FormReactive } from '@app/shared/forms/form-reactive'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9 import { forkJoin, Subject, Subscription } from 'rxjs'
10 import { SelectItem } from 'primeng/api'
11 import { first } from 'rxjs/operators'
12 import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
13 import { pick } from 'lodash-es'
14
15 @Component({
16 selector: 'my-account-video-settings',
17 templateUrl: './my-account-video-settings.component.html',
18 styleUrls: [ './my-account-video-settings.component.scss' ]
19 })
20 export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit, OnDestroy {
21 @Input() user: User = null
22 @Input() reactiveUpdate = false
23 @Input() notifyOnUpdate = true
24 @Input() userInformationLoaded: Subject<any>
25
26 languageItems: SelectItem[] = []
27 defaultNSFWPolicy: NSFWPolicyType
28 formValuesWatcher: Subscription
29
30 constructor (
31 protected formValidatorService: FormValidatorService,
32 private authService: AuthService,
33 private notifier: Notifier,
34 private userService: UserService,
35 private serverService: ServerService,
36 private i18n: I18n
37 ) {
38 super()
39 }
40
41 ngOnInit () {
42 let oldForm: any
43
44 this.buildForm({
45 nsfwPolicy: null,
46 webTorrentEnabled: null,
47 autoPlayVideo: null,
48 autoPlayNextVideo: null,
49 videoLanguages: null
50 })
51
52 forkJoin([
53 this.serverService.getVideoLanguages(),
54 this.serverService.getConfig(),
55 this.userInformationLoaded.pipe(first())
56 ]).subscribe(([ languages, config ]) => {
57 this.languageItems = [ { label: this.i18n('Unknown language'), value: '_unknown' } ]
58 this.languageItems = this.languageItems
59 .concat(languages.map(l => ({ label: l.label, value: l.id })))
60
61 const videoLanguages = this.user.videoLanguages
62 ? this.user.videoLanguages
63 : this.languageItems.map(l => l.value)
64
65 this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy
66
67 this.form.patchValue({
68 nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy,
69 webTorrentEnabled: this.user.webTorrentEnabled,
70 autoPlayVideo: this.user.autoPlayVideo === true,
71 autoPlayNextVideo: this.user.autoPlayNextVideo,
72 videoLanguages
73 })
74
75 if (this.reactiveUpdate) {
76 oldForm = { ...this.form.value }
77 this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => {
78 const updatedKey = Object.keys(formValue).find(k => formValue[k] !== oldForm[k])
79 oldForm = { ...this.form.value }
80 this.updateDetails([updatedKey])
81 })
82 }
83 })
84 }
85
86 ngOnDestroy () {
87 this.formValuesWatcher?.unsubscribe()
88 }
89
90 updateDetails (onlyKeys?: string[]) {
91 const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
92 const webTorrentEnabled = this.form.value['webTorrentEnabled']
93 const autoPlayVideo = this.form.value['autoPlayVideo']
94 const autoPlayNextVideo = this.form.value['autoPlayNextVideo']
95
96 let videoLanguages: string[] = this.form.value['videoLanguages']
97 if (Array.isArray(videoLanguages)) {
98 if (videoLanguages.length === this.languageItems.length) {
99 videoLanguages = null // null means "All"
100 } else if (videoLanguages.length > 20) {
101 this.notifier.error('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.')
102 return
103 } else if (videoLanguages.length === 0) {
104 this.notifier.error('You need to enabled at least 1 video language.')
105 return
106 }
107 }
108
109 let details: UserUpdateMe = {
110 nsfwPolicy,
111 webTorrentEnabled,
112 autoPlayVideo,
113 autoPlayNextVideo,
114 videoLanguages
115 }
116
117 if (onlyKeys) details = pick(details, onlyKeys)
118
119 if (this.authService.isLoggedIn()) {
120 this.userService.updateMyProfile(details).subscribe(
121 () => {
122 this.authService.refreshUserInformation()
123
124 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Video settings updated.'))
125 },
126
127 err => this.notifier.error(err.message)
128 )
129 } else {
130 this.userService.updateMyAnonymousProfile(details)
131 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Display/Video settings updated.'))
132 }
133 }
134
135 getDefaultVideoLanguageLabel () {
136 return this.i18n('No language')
137 }
138
139 getSelectedVideoLanguageLabel () {
140 return this.i18n('{{\'{0} languages selected')
141 }
142 }