1 import { Subject, Subscription } from 'rxjs'
2 import { Component, Input, OnDestroy, OnInit } from '@angular/core'
3 import { AuthService, Notifier, ServerService, UserService } from '@app/core'
4 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
5 import { ServerConfig, 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: ServerConfig
23 protected formValidatorService: FormValidatorService,
24 private authService: AuthService,
25 private notifier: Notifier,
26 private userService: UserService,
27 private serverService: ServerService
32 get availableThemes () {
33 return this.serverConfig.theme.registered
38 this.serverConfig = this.serverService.getTmpConfig()
39 this.serverService.getConfig()
40 .subscribe(config => this.serverConfig = config)
46 this.userInformationLoaded
48 this.form.patchValue({
49 theme: this.user.theme
52 if (this.reactiveUpdate) {
53 this.formValuesWatcher = this.form.valueChanges.subscribe(val => this.updateInterfaceSettings())
59 this.formValuesWatcher?.unsubscribe()
62 updateInterfaceSettings () {
63 const theme = this.form.value['theme']
65 const details: UserUpdateMe = {
69 if (this.authService.isLoggedIn()) {
70 this.userService.updateMyProfile(details).subscribe(
72 this.authService.refreshUserInformation()
74 if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`)
77 err => this.notifier.error(err.message)
80 this.userService.updateMyAnonymousProfile(details)
81 if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`)