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