]>
Commit | Line | Data |
---|---|---|
1 | import { Component, Input, OnInit, OnDestroy } from '@angular/core' | |
2 | import { Notifier, ServerService } from '@app/core' | |
3 | import { ServerConfig, UserUpdateMe } from '../../../../../../shared' | |
4 | import { AuthService } from '../../../core' | |
5 | import { FormReactive } from '../../../shared/forms/form-reactive' | |
6 | import { User, UserService } from '../../../shared/users' | |
7 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
8 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
9 | import { Subject, Subscription } from 'rxjs' | |
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 | }) | |
16 | export class MyAccountInterfaceSettingsComponent extends FormReactive implements OnInit, OnDestroy { | |
17 | @Input() user: User = null | |
18 | @Input() reactiveUpdate = false | |
19 | @Input() notifyOnUpdate = true | |
20 | @Input() userInformationLoaded: Subject<any> | |
21 | ||
22 | formValuesWatcher: Subscription | |
23 | ||
24 | private serverConfig: ServerConfig | |
25 | ||
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 () { | |
38 | return this.serverConfig.theme.registered | |
39 | .map(t => t.name) | |
40 | } | |
41 | ||
42 | ngOnInit () { | |
43 | this.serverConfig = this.serverService.getTmpConfig() | |
44 | this.serverService.getConfig() | |
45 | .subscribe(config => this.serverConfig = config) | |
46 | ||
47 | this.buildForm({ | |
48 | theme: null | |
49 | }) | |
50 | ||
51 | this.userInformationLoaded | |
52 | .subscribe(() => { | |
53 | this.form.patchValue({ | |
54 | theme: this.user.theme | |
55 | }) | |
56 | ||
57 | if (this.reactiveUpdate) { | |
58 | this.formValuesWatcher = this.form.valueChanges.subscribe(val => this.updateInterfaceSettings()) | |
59 | } | |
60 | }) | |
61 | } | |
62 | ||
63 | ngOnDestroy () { | |
64 | this.formValuesWatcher?.unsubscribe() | |
65 | } | |
66 | ||
67 | updateInterfaceSettings () { | |
68 | const theme = this.form.value['theme'] | |
69 | ||
70 | const details: UserUpdateMe = { | |
71 | theme | |
72 | } | |
73 | ||
74 | if (this.authService.isLoggedIn()) { | |
75 | this.userService.updateMyProfile(details).subscribe( | |
76 | () => { | |
77 | this.authService.refreshUserInformation() | |
78 | ||
79 | if (this.notifyOnUpdate) this.notifier.success(this.i18n('Interface settings updated.')) | |
80 | }, | |
81 | ||
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 | } | |
88 | } | |
89 | } |