]>
Commit | Line | Data |
---|---|---|
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' | |
11 | ||
12 | @Component({ | |
13 | selector: 'my-account-video-settings', | |
14 | templateUrl: './my-account-video-settings.component.html', | |
15 | styleUrls: [ './my-account-video-settings.component.scss' ] | |
16 | }) | |
17 | export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit { | |
18 | @Input() user: User = null | |
19 | @Input() userInformationLoaded: Subject<any> | |
20 | ||
21 | languageItems: SelectItem[] = [] | |
22 | ||
23 | constructor ( | |
24 | protected formValidatorService: FormValidatorService, | |
25 | private authService: AuthService, | |
26 | private notifier: Notifier, | |
27 | private userService: UserService, | |
28 | private serverService: ServerService, | |
29 | private i18n: I18n | |
30 | ) { | |
31 | super() | |
32 | } | |
33 | ||
34 | ngOnInit () { | |
35 | this.buildForm({ | |
36 | nsfwPolicy: null, | |
37 | webTorrentEnabled: null, | |
38 | autoPlayVideo: null, | |
39 | autoPlayNextVideo: null, | |
40 | videoLanguages: null | |
41 | }) | |
42 | ||
43 | forkJoin([ | |
44 | this.serverService.getVideoLanguages(), | |
45 | this.userInformationLoaded.pipe(first()) | |
46 | ]).subscribe(([ languages ]) => { | |
47 | this.languageItems = [ { label: this.i18n('Unknown language'), value: '_unknown' } ] | |
48 | this.languageItems = this.languageItems | |
49 | .concat(languages.map(l => ({ label: l.label, value: l.id }))) | |
50 | ||
51 | const videoLanguages = this.user.videoLanguages | |
52 | ? this.user.videoLanguages | |
53 | : this.languageItems.map(l => l.value) | |
54 | ||
55 | this.form.patchValue({ | |
56 | nsfwPolicy: this.user.nsfwPolicy, | |
57 | webTorrentEnabled: this.user.webTorrentEnabled, | |
58 | autoPlayVideo: this.user.autoPlayVideo === true, | |
59 | autoPlayNextVideo: this.user.autoPlayNextVideo, | |
60 | videoLanguages | |
61 | }) | |
62 | }) | |
63 | } | |
64 | ||
65 | updateDetails () { | |
66 | const nsfwPolicy = this.form.value[ 'nsfwPolicy' ] | |
67 | const webTorrentEnabled = this.form.value['webTorrentEnabled'] | |
68 | const autoPlayVideo = this.form.value['autoPlayVideo'] | |
69 | const autoPlayNextVideo = this.form.value['autoPlayNextVideo'] | |
70 | ||
71 | let videoLanguages: string[] = this.form.value['videoLanguages'] | |
72 | if (Array.isArray(videoLanguages)) { | |
73 | if (videoLanguages.length === this.languageItems.length) { | |
74 | videoLanguages = null // null means "All" | |
75 | } else if (videoLanguages.length > 20) { | |
76 | this.notifier.error('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.') | |
77 | return | |
78 | } else if (videoLanguages.length === 0) { | |
79 | this.notifier.error('You need to enabled at least 1 video language.') | |
80 | return | |
81 | } | |
82 | } | |
83 | ||
84 | const details: UserUpdateMe = { | |
85 | nsfwPolicy, | |
86 | webTorrentEnabled, | |
87 | autoPlayVideo, | |
88 | autoPlayNextVideo, | |
89 | videoLanguages | |
90 | } | |
91 | ||
92 | this.userService.updateMyProfile(details).subscribe( | |
93 | () => { | |
94 | this.notifier.success(this.i18n('Video settings updated.')) | |
95 | ||
96 | this.authService.refreshUserInformation() | |
97 | }, | |
98 | ||
99 | err => this.notifier.error(err.message) | |
100 | ) | |
101 | } | |
102 | ||
103 | getDefaultVideoLanguageLabel () { | |
104 | return this.i18n('No language') | |
105 | } | |
106 | ||
107 | getSelectedVideoLanguageLabel () { | |
108 | return this.i18n('{{\'{0} languages selected') | |
109 | } | |
110 | } |