]>
Commit | Line | Data |
---|---|---|
d3217560 | 1 | import { Subject, Subscription } from 'rxjs' |
67ed6552 C |
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' | |
7cd4d2ba C |
7 | |
8 | @Component({ | |
67ed6552 C |
9 | selector: 'my-user-interface-settings', |
10 | templateUrl: './user-interface-settings.component.html', | |
11 | styleUrls: [ './user-interface-settings.component.scss' ] | |
7cd4d2ba | 12 | }) |
67ed6552 | 13 | export class UserInterfaceSettingsComponent extends FormReactive implements OnInit, OnDestroy { |
7cd4d2ba | 14 | @Input() user: User = null |
d3217560 RK |
15 | @Input() reactiveUpdate = false |
16 | @Input() notifyOnUpdate = true | |
7cd4d2ba C |
17 | @Input() userInformationLoaded: Subject<any> |
18 | ||
d3217560 RK |
19 | formValuesWatcher: Subscription |
20 | ||
ba430d75 C |
21 | private serverConfig: ServerConfig |
22 | ||
7cd4d2ba C |
23 | constructor ( |
24 | protected formValidatorService: FormValidatorService, | |
25 | private authService: AuthService, | |
26 | private notifier: Notifier, | |
27 | private userService: UserService, | |
28 | private serverService: ServerService, | |
29 | private i18n: I18n | |
30 | ) { | |
31 | super() | |
32 | } | |
33 | ||
34 | get availableThemes () { | |
ba430d75 | 35 | return this.serverConfig.theme.registered |
ffb321be | 36 | .map(t => t.name) |
7cd4d2ba C |
37 | } |
38 | ||
39 | ngOnInit () { | |
ba430d75 C |
40 | this.serverConfig = this.serverService.getTmpConfig() |
41 | this.serverService.getConfig() | |
42 | .subscribe(config => this.serverConfig = config) | |
43 | ||
7cd4d2ba C |
44 | this.buildForm({ |
45 | theme: null | |
46 | }) | |
47 | ||
48 | this.userInformationLoaded | |
49 | .subscribe(() => { | |
50 | this.form.patchValue({ | |
51 | theme: this.user.theme | |
52 | }) | |
d3217560 RK |
53 | |
54 | if (this.reactiveUpdate) { | |
55 | this.formValuesWatcher = this.form.valueChanges.subscribe(val => this.updateInterfaceSettings()) | |
56 | } | |
7cd4d2ba C |
57 | }) |
58 | } | |
59 | ||
d3217560 RK |
60 | ngOnDestroy () { |
61 | this.formValuesWatcher?.unsubscribe() | |
62 | } | |
63 | ||
7cd4d2ba C |
64 | updateInterfaceSettings () { |
65 | const theme = this.form.value['theme'] | |
66 | ||
67 | const details: UserUpdateMe = { | |
68 | theme | |
69 | } | |
70 | ||
d3217560 RK |
71 | if (this.authService.isLoggedIn()) { |
72 | this.userService.updateMyProfile(details).subscribe( | |
73 | () => { | |
74 | this.authService.refreshUserInformation() | |
7cd4d2ba | 75 | |
d3217560 RK |
76 | if (this.notifyOnUpdate) this.notifier.success(this.i18n('Interface settings updated.')) |
77 | }, | |
7cd4d2ba | 78 | |
d3217560 RK |
79 | err => this.notifier.error(err.message) |
80 | ) | |
81 | } else { | |
82 | this.userService.updateMyAnonymousProfile(details) | |
83 | if (this.notifyOnUpdate) this.notifier.success(this.i18n('Interface settings updated.')) | |
84 | } | |
7cd4d2ba C |
85 | } |
86 | } |