]>
Commit | Line | Data |
---|---|---|
d3217560 | 1 | import { Subject, Subscription } from 'rxjs' |
67ed6552 | 2 | import { Component, Input, OnDestroy, OnInit } from '@angular/core' |
5e93a6d1 | 3 | import { AuthService, Notifier, ServerService, ThemeService, UserService } from '@app/core' |
5c5bcea2 | 4 | import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' |
2989628b | 5 | import { HTMLServerConfig, User, UserUpdateMe } from '@shared/models' |
00fe5d61 | 6 | import { SelectOptionsItem } from 'src/types' |
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 | ||
00fe5d61 | 19 | availableThemes: SelectOptionsItem[] |
d3217560 RK |
20 | formValuesWatcher: Subscription |
21 | ||
2989628b | 22 | private serverConfig: HTMLServerConfig |
ba430d75 | 23 | |
7cd4d2ba | 24 | constructor ( |
5c5bcea2 | 25 | protected formReactiveService: FormReactiveService, |
7cd4d2ba C |
26 | private authService: AuthService, |
27 | private notifier: Notifier, | |
28 | private userService: UserService, | |
5e93a6d1 | 29 | private themeService: ThemeService, |
66357162 | 30 | private serverService: ServerService |
7cd4d2ba C |
31 | ) { |
32 | super() | |
33 | } | |
34 | ||
5e93a6d1 C |
35 | get instanceName () { |
36 | return this.serverConfig.instance.name | |
7cd4d2ba C |
37 | } |
38 | ||
39 | ngOnInit () { | |
2989628b | 40 | this.serverConfig = this.serverService.getHTMLConfig() |
ba430d75 | 41 | |
00fe5d61 C |
42 | this.availableThemes = this.themeService.buildAvailableThemes() |
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) { | |
4b57ebdf | 55 | this.formValuesWatcher = this.form.valueChanges.subscribe(() => this.updateInterfaceSettings()) |
d3217560 | 56 | } |
7cd4d2ba C |
57 | }) |
58 | } | |
59 | ||
d3217560 RK |
60 | ngOnDestroy () { |
61 | this.formValuesWatcher?.unsubscribe() | |
62 | } | |
63 | ||
ff6a266e | 64 | getDefaultThemeLabel () { |
5e93a6d1 C |
65 | return this.themeService.getDefaultThemeLabel() |
66 | } | |
67 | ||
5e93a6d1 | 68 | getDefaultInstanceThemeLabel () { |
ff6a266e C |
69 | const theme = this.serverConfig.theme.default |
70 | ||
5e93a6d1 C |
71 | if (theme === 'default') { |
72 | return this.getDefaultThemeLabel() | |
73 | } | |
ff6a266e C |
74 | |
75 | return theme | |
76 | } | |
77 | ||
7cd4d2ba C |
78 | updateInterfaceSettings () { |
79 | const theme = this.form.value['theme'] | |
80 | ||
81 | const details: UserUpdateMe = { | |
82 | theme | |
83 | } | |
84 | ||
d3217560 | 85 | if (this.authService.isLoggedIn()) { |
1378c0d3 C |
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 | |
d3217560 | 98 | } |
1378c0d3 C |
99 | |
100 | this.userService.updateMyAnonymousProfile(details) | |
101 | if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`) | |
7cd4d2ba C |
102 | } |
103 | } |