]>
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' | |
ff6a266e | 5 | import { capitalizeFirstLetter } from '@root-helpers/string' |
2989628b | 6 | import { HTMLServerConfig, 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 | ||
ff6a266e C |
21 | defaultThemeLabel = $localize`Light/Orange` |
22 | ||
2989628b | 23 | private serverConfig: HTMLServerConfig |
ba430d75 | 24 | |
7cd4d2ba C |
25 | constructor ( |
26 | protected formValidatorService: FormValidatorService, | |
27 | private authService: AuthService, | |
28 | private notifier: Notifier, | |
29 | private userService: UserService, | |
66357162 | 30 | private serverService: ServerService |
7cd4d2ba C |
31 | ) { |
32 | super() | |
33 | } | |
34 | ||
35 | get availableThemes () { | |
ba430d75 | 36 | return this.serverConfig.theme.registered |
ffb321be | 37 | .map(t => t.name) |
7cd4d2ba C |
38 | } |
39 | ||
40 | ngOnInit () { | |
2989628b | 41 | this.serverConfig = this.serverService.getHTMLConfig() |
ba430d75 | 42 | |
7cd4d2ba C |
43 | this.buildForm({ |
44 | theme: null | |
45 | }) | |
46 | ||
47 | this.userInformationLoaded | |
48 | .subscribe(() => { | |
49 | this.form.patchValue({ | |
50 | theme: this.user.theme | |
51 | }) | |
d3217560 RK |
52 | |
53 | if (this.reactiveUpdate) { | |
4b57ebdf | 54 | this.formValuesWatcher = this.form.valueChanges.subscribe(() => this.updateInterfaceSettings()) |
d3217560 | 55 | } |
7cd4d2ba C |
56 | }) |
57 | } | |
58 | ||
d3217560 RK |
59 | ngOnDestroy () { |
60 | this.formValuesWatcher?.unsubscribe() | |
61 | } | |
62 | ||
ff6a266e C |
63 | getDefaultThemeLabel () { |
64 | const theme = this.serverConfig.theme.default | |
65 | ||
66 | if (theme === 'default') return this.defaultThemeLabel | |
67 | ||
68 | return theme | |
69 | } | |
70 | ||
71 | capitalizeFirstLetter (str: string) { | |
72 | return capitalizeFirstLetter(str) | |
73 | } | |
74 | ||
7cd4d2ba C |
75 | updateInterfaceSettings () { |
76 | const theme = this.form.value['theme'] | |
77 | ||
78 | const details: UserUpdateMe = { | |
79 | theme | |
80 | } | |
81 | ||
d3217560 | 82 | if (this.authService.isLoggedIn()) { |
1378c0d3 C |
83 | this.userService.updateMyProfile(details) |
84 | .subscribe({ | |
85 | next: () => { | |
86 | this.authService.refreshUserInformation() | |
87 | ||
88 | if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`) | |
89 | }, | |
90 | ||
91 | error: err => this.notifier.error(err.message) | |
92 | }) | |
93 | ||
94 | return | |
d3217560 | 95 | } |
1378c0d3 C |
96 | |
97 | this.userService.updateMyAnonymousProfile(details) | |
98 | if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`) | |
7cd4d2ba C |
99 | } |
100 | } |