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