]>
Commit | Line | Data |
---|---|---|
d3217560 | 1 | import { Component, Input, OnInit, OnDestroy } from '@angular/core' |
7cd4d2ba | 2 | import { Notifier, ServerService } from '@app/core' |
ba430d75 | 3 | import { ServerConfig, UserUpdateMe } from '../../../../../../shared' |
7cd4d2ba | 4 | import { AuthService } from '../../../core' |
d3217560 RK |
5 | import { FormReactive } from '../../../shared/forms/form-reactive' |
6 | import { User, UserService } from '../../../shared/users' | |
7cd4d2ba C |
7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
8 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
d3217560 | 9 | import { Subject, Subscription } from 'rxjs' |
7cd4d2ba C |
10 | |
11 | @Component({ | |
12 | selector: 'my-account-interface-settings', | |
13 | templateUrl: './my-account-interface-settings.component.html', | |
14 | styleUrls: [ './my-account-interface-settings.component.scss' ] | |
15 | }) | |
d3217560 | 16 | export class MyAccountInterfaceSettingsComponent extends FormReactive implements OnInit, OnDestroy { |
7cd4d2ba | 17 | @Input() user: User = null |
d3217560 RK |
18 | @Input() reactiveUpdate = false |
19 | @Input() notifyOnUpdate = true | |
7cd4d2ba C |
20 | @Input() userInformationLoaded: Subject<any> |
21 | ||
d3217560 RK |
22 | formValuesWatcher: Subscription |
23 | ||
ba430d75 C |
24 | private serverConfig: ServerConfig |
25 | ||
7cd4d2ba C |
26 | constructor ( |
27 | protected formValidatorService: FormValidatorService, | |
28 | private authService: AuthService, | |
29 | private notifier: Notifier, | |
30 | private userService: UserService, | |
31 | private serverService: ServerService, | |
32 | private i18n: I18n | |
33 | ) { | |
34 | super() | |
35 | } | |
36 | ||
37 | get availableThemes () { | |
ba430d75 | 38 | return this.serverConfig.theme.registered |
ffb321be | 39 | .map(t => t.name) |
7cd4d2ba C |
40 | } |
41 | ||
42 | ngOnInit () { | |
ba430d75 C |
43 | this.serverConfig = this.serverService.getTmpConfig() |
44 | this.serverService.getConfig() | |
45 | .subscribe(config => this.serverConfig = config) | |
46 | ||
7cd4d2ba C |
47 | this.buildForm({ |
48 | theme: null | |
49 | }) | |
50 | ||
51 | this.userInformationLoaded | |
52 | .subscribe(() => { | |
53 | this.form.patchValue({ | |
54 | theme: this.user.theme | |
55 | }) | |
d3217560 RK |
56 | |
57 | if (this.reactiveUpdate) { | |
58 | this.formValuesWatcher = this.form.valueChanges.subscribe(val => this.updateInterfaceSettings()) | |
59 | } | |
7cd4d2ba C |
60 | }) |
61 | } | |
62 | ||
d3217560 RK |
63 | ngOnDestroy () { |
64 | this.formValuesWatcher?.unsubscribe() | |
65 | } | |
66 | ||
7cd4d2ba C |
67 | updateInterfaceSettings () { |
68 | const theme = this.form.value['theme'] | |
69 | ||
70 | const details: UserUpdateMe = { | |
71 | theme | |
72 | } | |
73 | ||
d3217560 RK |
74 | if (this.authService.isLoggedIn()) { |
75 | this.userService.updateMyProfile(details).subscribe( | |
76 | () => { | |
77 | this.authService.refreshUserInformation() | |
7cd4d2ba | 78 | |
d3217560 RK |
79 | if (this.notifyOnUpdate) this.notifier.success(this.i18n('Interface settings updated.')) |
80 | }, | |
7cd4d2ba | 81 | |
d3217560 RK |
82 | err => this.notifier.error(err.message) |
83 | ) | |
84 | } else { | |
85 | this.userService.updateMyAnonymousProfile(details) | |
86 | if (this.notifyOnUpdate) this.notifier.success(this.i18n('Interface settings updated.')) | |
87 | } | |
7cd4d2ba C |
88 | } |
89 | } |