]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-account/my-account-settings/my-account-settings.component.ts
Refactor my actor avatar edit
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-settings.component.ts
1 import { ViewportScroller } from '@angular/common'
2 import { HttpErrorResponse } from '@angular/common/http'
3 import { AfterViewChecked, Component, OnInit } from '@angular/core'
4 import { AuthService, Notifier, User, UserService } from '@app/core'
5 import { genericUploadErrorHandler } from '@app/helpers'
6 import { shallowCopy } from '@shared/core-utils'
7
8 @Component({
9 selector: 'my-account-settings',
10 templateUrl: './my-account-settings.component.html',
11 styleUrls: [ './my-account-settings.component.scss' ]
12 })
13 export class MyAccountSettingsComponent implements OnInit, AfterViewChecked {
14 user: User = null
15
16 private lastScrollHash: string
17
18 constructor (
19 private viewportScroller: ViewportScroller,
20 private userService: UserService,
21 private authService: AuthService,
22 private notifier: Notifier
23 ) {}
24
25 get userInformationLoaded () {
26 return this.authService.userInformationLoaded
27 }
28
29 ngOnInit () {
30 this.user = this.authService.getUser()
31 }
32
33 ngAfterViewChecked () {
34 if (window.location.hash && window.location.hash !== this.lastScrollHash) {
35 this.viewportScroller.scrollToAnchor(window.location.hash.replace('#', ''))
36
37 this.lastScrollHash = window.location.hash
38 }
39 }
40
41 onAvatarChange (formData: FormData) {
42 this.userService.changeAvatar(formData)
43 .subscribe({
44 next: data => {
45 this.notifier.success($localize`Avatar changed.`)
46
47 this.user.updateAccountAvatar(data.avatars)
48
49 // So my-actor-avatar component detects changes
50 this.user.account = shallowCopy(this.user.account)
51 },
52
53 error: (err: HttpErrorResponse) => genericUploadErrorHandler({
54 err,
55 name: $localize`avatar`,
56 notifier: this.notifier
57 })
58 })
59 }
60
61 onAvatarDelete () {
62 this.userService.deleteAvatar()
63 .subscribe({
64 next: () => {
65 this.notifier.success($localize`Avatar deleted.`)
66
67 this.user.updateAccountAvatar()
68
69 // So my-actor-avatar component detects changes
70 this.user.account = shallowCopy(this.user.account)
71 },
72
73 error: (err: HttpErrorResponse) => this.notifier.error(err.message)
74 })
75 }
76 }