Properly scroll to anchors in links, especially in admin config
[oweals/peertube.git] / client / src / app / +my-account / my-account-settings / my-account-settings.component.ts
1 import { Component, OnInit, AfterViewChecked } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { BytesPipe } from 'ngx-pipes'
4 import { AuthService } from '../../core'
5 import { User } from '../../shared'
6 import { UserService } from '../../shared/users'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { ViewportScroller } from '@angular/common'
9
10 @Component({
11   selector: 'my-account-settings',
12   templateUrl: './my-account-settings.component.html',
13   styleUrls: [ './my-account-settings.component.scss' ]
14 })
15 export class MyAccountSettingsComponent implements OnInit, AfterViewChecked {
16   user: User = null
17
18   userVideoQuota = '0'
19   userVideoQuotaUsed = 0
20
21   userVideoQuotaDaily = '0'
22   userVideoQuotaUsedDaily = 0
23
24   constructor (
25     private viewportScroller: ViewportScroller,
26     private userService: UserService,
27     private authService: AuthService,
28     private notifier: Notifier,
29     private i18n: I18n
30   ) {}
31
32   get userInformationLoaded () {
33     return this.authService.userInformationLoaded
34   }
35
36   ngOnInit () {
37     this.user = this.authService.getUser()
38
39     this.authService.userInformationLoaded.subscribe(
40       () => {
41         if (this.user.videoQuota !== -1) {
42           this.userVideoQuota = new BytesPipe().transform(this.user.videoQuota, 0).toString()
43         } else {
44           this.userVideoQuota = this.i18n('Unlimited')
45         }
46
47         if (this.user.videoQuotaDaily !== -1) {
48           this.userVideoQuotaDaily = new BytesPipe().transform(this.user.videoQuotaDaily, 0).toString()
49         } else {
50           this.userVideoQuotaDaily = this.i18n('Unlimited')
51         }
52       }
53     )
54
55     this.userService.getMyVideoQuotaUsed()
56       .subscribe(data => {
57         this.userVideoQuotaUsed = data.videoQuotaUsed
58         this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily
59       })
60   }
61
62   ngAfterViewChecked () {
63     if (window.location.hash) this.viewportScroller.scrollToAnchor(window.location.hash.replace('#', ''))
64   }
65
66   onAvatarChange (formData: FormData) {
67     this.userService.changeAvatar(formData)
68       .subscribe(
69         data => {
70           this.notifier.success(this.i18n('Avatar changed.'))
71
72           this.user.updateAccountAvatar(data.avatar)
73         },
74
75         err => this.notifier.error(err.message)
76       )
77   }
78
79   hasDailyQuota () {
80     return this.user.videoQuotaDaily !== -1
81   }
82 }