]>
Commit | Line | Data |
---|---|---|
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 { capitalizeFirstLetter } from '@root-helpers/string' | |
6 | import { HTMLServerConfig, User, UserUpdateMe } from '@shared/models' | |
7 | ||
8 | @Component({ | |
9 | selector: 'my-user-interface-settings', | |
10 | templateUrl: './user-interface-settings.component.html', | |
11 | styleUrls: [ './user-interface-settings.component.scss' ] | |
12 | }) | |
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> | |
18 | ||
19 | formValuesWatcher: Subscription | |
20 | ||
21 | defaultThemeLabel = $localize`Light/Orange` | |
22 | ||
23 | private serverConfig: HTMLServerConfig | |
24 | ||
25 | constructor ( | |
26 | protected formValidatorService: FormValidatorService, | |
27 | private authService: AuthService, | |
28 | private notifier: Notifier, | |
29 | private userService: UserService, | |
30 | private serverService: ServerService | |
31 | ) { | |
32 | super() | |
33 | } | |
34 | ||
35 | get availableThemes () { | |
36 | return this.serverConfig.theme.registered | |
37 | .map(t => t.name) | |
38 | } | |
39 | ||
40 | ngOnInit () { | |
41 | this.serverConfig = this.serverService.getHTMLConfig() | |
42 | ||
43 | this.buildForm({ | |
44 | theme: null | |
45 | }) | |
46 | ||
47 | this.userInformationLoaded | |
48 | .subscribe(() => { | |
49 | this.form.patchValue({ | |
50 | theme: this.user.theme | |
51 | }) | |
52 | ||
53 | if (this.reactiveUpdate) { | |
54 | this.formValuesWatcher = this.form.valueChanges.subscribe(() => this.updateInterfaceSettings()) | |
55 | } | |
56 | }) | |
57 | } | |
58 | ||
59 | ngOnDestroy () { | |
60 | this.formValuesWatcher?.unsubscribe() | |
61 | } | |
62 | ||
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 | ||
75 | updateInterfaceSettings () { | |
76 | const theme = this.form.value['theme'] | |
77 | ||
78 | const details: UserUpdateMe = { | |
79 | theme | |
80 | } | |
81 | ||
82 | if (this.authService.isLoggedIn()) { | |
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 | |
95 | } | |
96 | ||
97 | this.userService.updateMyAnonymousProfile(details) | |
98 | if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`) | |
99 | } | |
100 | } |