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, FormReactiveService } from '@app/shared/shared-forms'
5 import { HTMLServerConfig, User, UserUpdateMe } from '@shared/models'
6 import { SelectOptionsItem } from 'src/types'
9 selector: 'my-user-interface-settings',
10 templateUrl: './user-interface-settings.component.html',
11 styleUrls: [ './user-interface-settings.component.scss' ]
13 export class UserInterfaceSettingsComponent extends FormReactive implements OnInit, OnDestroy {
14 @Input() user: User = null
15 @Input() reactiveUpdate = false
16 @Input() notifyOnUpdate = true
17 @Input() userInformationLoaded: Subject<any>
19 availableThemes: SelectOptionsItem[]
20 formValuesWatcher: Subscription
22 private serverConfig: HTMLServerConfig
25 protected formReactiveService: FormReactiveService,
26 private authService: AuthService,
27 private notifier: Notifier,
28 private userService: UserService,
29 private themeService: ThemeService,
30 private serverService: ServerService
36 return this.serverConfig.instance.name
40 this.serverConfig = this.serverService.getHTMLConfig()
42 this.availableThemes = this.themeService.buildAvailableThemes()
48 this.userInformationLoaded
50 this.form.patchValue({
51 theme: this.user.theme
54 if (this.reactiveUpdate) {
55 this.formValuesWatcher = this.form.valueChanges.subscribe(() => this.updateInterfaceSettings())
61 this.formValuesWatcher?.unsubscribe()
64 getDefaultThemeLabel () {
65 return this.themeService.getDefaultThemeLabel()
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.`)