]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-user-settings/user-interface-settings.component.ts
Improve theme label
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-settings / user-interface-settings.component.ts
CommitLineData
d3217560 1import { Subject, Subscription } from 'rxjs'
67ed6552 2import { Component, Input, OnDestroy, OnInit } from '@angular/core'
5e93a6d1 3import { AuthService, Notifier, ServerService, ThemeService, UserService } from '@app/core'
67ed6552 4import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
2989628b 5import { HTMLServerConfig, User, UserUpdateMe } from '@shared/models'
7cd4d2ba
C
6
7@Component({
67ed6552
C
8 selector: 'my-user-interface-settings',
9 templateUrl: './user-interface-settings.component.html',
10 styleUrls: [ './user-interface-settings.component.scss' ]
7cd4d2ba 11})
67ed6552 12export class UserInterfaceSettingsComponent extends FormReactive implements OnInit, OnDestroy {
7cd4d2ba 13 @Input() user: User = null
d3217560
RK
14 @Input() reactiveUpdate = false
15 @Input() notifyOnUpdate = true
7cd4d2ba
C
16 @Input() userInformationLoaded: Subject<any>
17
d3217560
RK
18 formValuesWatcher: Subscription
19
2989628b 20 private serverConfig: HTMLServerConfig
ba430d75 21
7cd4d2ba
C
22 constructor (
23 protected formValidatorService: FormValidatorService,
24 private authService: AuthService,
25 private notifier: Notifier,
26 private userService: UserService,
5e93a6d1 27 private themeService: ThemeService,
66357162 28 private serverService: ServerService
7cd4d2ba
C
29 ) {
30 super()
31 }
32
5e93a6d1
C
33 get instanceName () {
34 return this.serverConfig.instance.name
7cd4d2ba
C
35 }
36
37 ngOnInit () {
2989628b 38 this.serverConfig = this.serverService.getHTMLConfig()
ba430d75 39
7cd4d2ba
C
40 this.buildForm({
41 theme: null
42 })
43
44 this.userInformationLoaded
45 .subscribe(() => {
46 this.form.patchValue({
47 theme: this.user.theme
48 })
d3217560
RK
49
50 if (this.reactiveUpdate) {
4b57ebdf 51 this.formValuesWatcher = this.form.valueChanges.subscribe(() => this.updateInterfaceSettings())
d3217560 52 }
7cd4d2ba
C
53 })
54 }
55
d3217560
RK
56 ngOnDestroy () {
57 this.formValuesWatcher?.unsubscribe()
58 }
59
ff6a266e 60 getDefaultThemeLabel () {
5e93a6d1
C
61 return this.themeService.getDefaultThemeLabel()
62 }
63
64 getAvailableThemes () {
65 return this.themeService.getAvailableThemeLabels()
66 }
67
68 getDefaultInstanceThemeLabel () {
ff6a266e
C
69 const theme = this.serverConfig.theme.default
70
5e93a6d1
C
71 if (theme === 'default') {
72 return this.getDefaultThemeLabel()
73 }
ff6a266e
C
74
75 return theme
76 }
77
7cd4d2ba
C
78 updateInterfaceSettings () {
79 const theme = this.form.value['theme']
80
81 const details: UserUpdateMe = {
82 theme
83 }
84
d3217560 85 if (this.authService.isLoggedIn()) {
1378c0d3
C
86 this.userService.updateMyProfile(details)
87 .subscribe({
88 next: () => {
89 this.authService.refreshUserInformation()
90
91 if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`)
92 },
93
94 error: err => this.notifier.error(err.message)
95 })
96
97 return
d3217560 98 }
1378c0d3
C
99
100 this.userService.updateMyAnonymousProfile(details)
101 if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`)
7cd4d2ba
C
102 }
103}