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