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