1 import { Component, Input, OnInit, OnDestroy } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { ServerConfig, UserUpdateMe } from '../../../../../../shared'
4 import { AuthService } from '../../../core'
5 import { FormReactive } from '../../../shared/forms/form-reactive'
6 import { User, UserService } from '../../../shared/users'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9 import { Subject, Subscription } from 'rxjs'
12 selector: 'my-account-interface-settings',
13 templateUrl: './my-account-interface-settings.component.html',
14 styleUrls: [ './my-account-interface-settings.component.scss' ]
16 export class MyAccountInterfaceSettingsComponent extends FormReactive implements OnInit, OnDestroy {
17 @Input() user: User = null
18 @Input() reactiveUpdate = false
19 @Input() notifyOnUpdate = true
20 @Input() userInformationLoaded: Subject<any>
22 formValuesWatcher: Subscription
24 private serverConfig: ServerConfig
27 protected formValidatorService: FormValidatorService,
28 private authService: AuthService,
29 private notifier: Notifier,
30 private userService: UserService,
31 private serverService: ServerService,
37 get availableThemes () {
38 return this.serverConfig.theme.registered
43 this.serverConfig = this.serverService.getTmpConfig()
44 this.serverService.getConfig()
45 .subscribe(config => this.serverConfig = config)
51 this.userInformationLoaded
53 this.form.patchValue({
54 theme: this.user.theme
57 if (this.reactiveUpdate) {
58 this.formValuesWatcher = this.form.valueChanges.subscribe(val => this.updateInterfaceSettings())
64 this.formValuesWatcher?.unsubscribe()
67 updateInterfaceSettings () {
68 const theme = this.form.value['theme']
70 const details: UserUpdateMe = {
74 if (this.authService.isLoggedIn()) {
75 this.userService.updateMyProfile(details).subscribe(
77 this.authService.refreshUserInformation()
79 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Interface settings updated.'))
82 err => this.notifier.error(err.message)
85 this.userService.updateMyAnonymousProfile(details)
86 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Interface settings updated.'))