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 { I18n } from '@ngx-translate/i18n-polyfill'
6 import { ServerConfig, User, UserUpdateMe } from '@shared/models'
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 formValuesWatcher: Subscription
21 private serverConfig: ServerConfig
24 protected formValidatorService: FormValidatorService,
25 private authService: AuthService,
26 private notifier: Notifier,
27 private userService: UserService,
28 private serverService: ServerService,
34 get availableThemes () {
35 return this.serverConfig.theme.registered
40 this.serverConfig = this.serverService.getTmpConfig()
41 this.serverService.getConfig()
42 .subscribe(config => this.serverConfig = config)
48 this.userInformationLoaded
50 this.form.patchValue({
51 theme: this.user.theme
54 if (this.reactiveUpdate) {
55 this.formValuesWatcher = this.form.valueChanges.subscribe(val => this.updateInterfaceSettings())
61 this.formValuesWatcher?.unsubscribe()
64 updateInterfaceSettings () {
65 const theme = this.form.value['theme']
67 const details: UserUpdateMe = {
71 if (this.authService.isLoggedIn()) {
72 this.userService.updateMyProfile(details).subscribe(
74 this.authService.refreshUserInformation()
76 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Interface settings updated.'))
79 err => this.notifier.error(err.message)
82 this.userService.updateMyAnonymousProfile(details)
83 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Interface settings updated.'))