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'
8 selector: 'my-user-interface-settings',
9 templateUrl: './user-interface-settings.component.html',
10 styleUrls: [ './user-interface-settings.component.scss' ]
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>
18 formValuesWatcher: Subscription
20 private serverConfig: HTMLServerConfig
23 protected formValidatorService: FormValidatorService,
24 private authService: AuthService,
25 private notifier: Notifier,
26 private userService: UserService,
27 private themeService: ThemeService,
28 private serverService: ServerService
34 return this.serverConfig.instance.name
38 this.serverConfig = this.serverService.getHTMLConfig()
44 this.userInformationLoaded
46 this.form.patchValue({
47 theme: this.user.theme
50 if (this.reactiveUpdate) {
51 this.formValuesWatcher = this.form.valueChanges.subscribe(() => this.updateInterfaceSettings())
57 this.formValuesWatcher?.unsubscribe()
60 getDefaultThemeLabel () {
61 return this.themeService.getDefaultThemeLabel()
64 getAvailableThemes () {
65 return this.themeService.getAvailableThemeLabels()
68 getDefaultInstanceThemeLabel () {
69 const theme = this.serverConfig.theme.default
71 if (theme === 'default') {
72 return this.getDefaultThemeLabel()
78 updateInterfaceSettings () {
79 const theme = this.form.value['theme']
81 const details: UserUpdateMe = {
85 if (this.authService.isLoggedIn()) {
86 this.userService.updateMyProfile(details)
89 this.authService.refreshUserInformation()
91 if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`)
94 error: err => this.notifier.error(err.message)
100 this.userService.updateMyAnonymousProfile(details)
101 if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`)