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