]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-user-settings/user-interface-settings.component.ts
Migrate to $localize
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-settings / user-interface-settings.component.ts
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 { ServerConfig, User, UserUpdateMe } from '@shared/models'
6
7 @Component({
8 selector: 'my-user-interface-settings',
9 templateUrl: './user-interface-settings.component.html',
10 styleUrls: [ './user-interface-settings.component.scss' ]
11 })
12 export class UserInterfaceSettingsComponent extends FormReactive implements OnInit, OnDestroy {
13 @Input() user: User = null
14 @Input() reactiveUpdate = false
15 @Input() notifyOnUpdate = true
16 @Input() userInformationLoaded: Subject<any>
17
18 formValuesWatcher: Subscription
19
20 private serverConfig: ServerConfig
21
22 constructor (
23 protected formValidatorService: FormValidatorService,
24 private authService: AuthService,
25 private notifier: Notifier,
26 private userService: UserService,
27 private serverService: ServerService
28 ) {
29 super()
30 }
31
32 get availableThemes () {
33 return this.serverConfig.theme.registered
34 .map(t => t.name)
35 }
36
37 ngOnInit () {
38 this.serverConfig = this.serverService.getTmpConfig()
39 this.serverService.getConfig()
40 .subscribe(config => this.serverConfig = config)
41
42 this.buildForm({
43 theme: null
44 })
45
46 this.userInformationLoaded
47 .subscribe(() => {
48 this.form.patchValue({
49 theme: this.user.theme
50 })
51
52 if (this.reactiveUpdate) {
53 this.formValuesWatcher = this.form.valueChanges.subscribe(val => this.updateInterfaceSettings())
54 }
55 })
56 }
57
58 ngOnDestroy () {
59 this.formValuesWatcher?.unsubscribe()
60 }
61
62 updateInterfaceSettings () {
63 const theme = this.form.value['theme']
64
65 const details: UserUpdateMe = {
66 theme
67 }
68
69 if (this.authService.isLoggedIn()) {
70 this.userService.updateMyProfile(details).subscribe(
71 () => {
72 this.authService.refreshUserInformation()
73
74 if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`)
75 },
76
77 err => this.notifier.error(err.message)
78 )
79 } else {
80 this.userService.updateMyAnonymousProfile(details)
81 if (this.notifyOnUpdate) this.notifier.success($localize`Interface settings updated.`)
82 }
83 }
84 }