]>
Commit | Line | Data |
---|---|---|
1 | import { Subject, Subscription } from 'rxjs' | |
2 | import { Component, Input, OnDestroy, OnInit } from '@angular/core' | |
3 | import { AuthService, Notifier, ServerService, ThemeService, UserService } from '@app/core' | |
4 | import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' | |
5 | import { HTMLServerConfig, User, UserUpdateMe } from '@shared/models' | |
6 | import { SelectOptionsItem } from 'src/types' | |
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 | availableThemes: SelectOptionsItem[] | |
20 | formValuesWatcher: Subscription | |
21 | ||
22 | private serverConfig: HTMLServerConfig | |
23 | ||
24 | constructor ( | |
25 | protected formReactiveService: FormReactiveService, | |
26 | private authService: AuthService, | |
27 | private notifier: Notifier, | |
28 | private userService: UserService, | |
29 | private themeService: ThemeService, | |
30 | private serverService: ServerService | |
31 | ) { | |
32 | super() | |
33 | } | |
34 | ||
35 | get instanceName () { | |
36 | return this.serverConfig.instance.name | |
37 | } | |
38 | ||
39 | ngOnInit () { | |
40 | this.serverConfig = this.serverService.getHTMLConfig() | |
41 | ||
42 | this.availableThemes = this.themeService.buildAvailableThemes() | |
43 | ||
44 | this.buildForm({ | |
45 | theme: null | |
46 | }) | |
47 | ||
48 | this.userInformationLoaded | |
49 | .subscribe(() => { | |
50 | this.form.patchValue({ | |
51 | theme: this.user.theme | |
52 | }) | |
53 | ||
54 | if (this.reactiveUpdate) { | |
55 | this.formValuesWatcher = this.form.valueChanges.subscribe(() => this.updateInterfaceSettings()) | |
56 | } | |
57 | }) | |
58 | } | |
59 | ||
60 | ngOnDestroy () { | |
61 | this.formValuesWatcher?.unsubscribe() | |
62 | } | |
63 | ||
64 | getDefaultThemeLabel () { | |
65 | return this.themeService.getDefaultThemeLabel() | |
66 | } | |
67 | ||
68 | getDefaultInstanceThemeLabel () { | |
69 | const theme = this.serverConfig.theme.default | |
70 | ||
71 | if (theme === 'default') { | |
72 | return this.getDefaultThemeLabel() | |
73 | } | |
74 | ||
75 | return theme | |
76 | } | |
77 | ||
78 | updateInterfaceSettings () { | |
79 | const theme = this.form.value['theme'] | |
80 | ||
81 | const details: UserUpdateMe = { | |
82 | theme | |
83 | } | |
84 | ||
85 | if (this.authService.isLoggedIn()) { | |
86 | this.userService.updateMyProfile(details) | |
87 | .subscribe({ | |
88 | next: () => { | |
89 | this.authService.refreshUserInformation() | |
90 | ||
91 | if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`) | |
92 | }, | |
93 | ||
94 | error: err => this.notifier.error(err.message) | |
95 | }) | |
96 | ||
97 | return | |
98 | } | |
99 | ||
100 | this.userService.updateMyAnonymousProfile(details) | |
101 | if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`) | |
102 | } | |
103 | } |