]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/signup/signup.component.ts
a40f42cb11b0a2b678b9d1874d5f2118b8a8a6a8
[github/Chocobozzz/PeerTube.git] / client / src / app / signup / signup.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { ServerService } from '@app/core/server'
4 import { NotificationsService } from 'angular2-notifications'
5 import { UserCreate } from '../../../../shared'
6 import { FormReactive, UserService, UserValidatorsService } from '../shared'
7 import { RedirectService } from '@app/core'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10
11 @Component({
12 selector: 'my-signup',
13 templateUrl: './signup.component.html',
14 styleUrls: [ './signup.component.scss' ]
15 })
16 export class SignupComponent extends FormReactive implements OnInit {
17 error: string = null
18 quotaHelpIndication = ''
19
20 private static getApproximateTime (seconds: number) {
21 const hours = Math.floor(seconds / 3600)
22 let pluralSuffix = ''
23 if (hours > 1) pluralSuffix = 's'
24 if (hours > 0) return `~ ${hours} hour${pluralSuffix}`
25
26 const minutes = Math.floor(seconds % 3600 / 60)
27 if (minutes > 1) pluralSuffix = 's'
28
29 return `~ ${minutes} minute${pluralSuffix}`
30 }
31
32 constructor (
33 protected formValidatorService: FormValidatorService,
34 private userValidatorsService: UserValidatorsService,
35 private router: Router,
36 private notificationsService: NotificationsService,
37 private userService: UserService,
38 private redirectService: RedirectService,
39 private serverService: ServerService,
40 private i18n: I18n
41 ) {
42 super()
43 }
44
45 get initialUserVideoQuota () {
46 return this.serverService.getConfig().user.videoQuota
47 }
48
49 ngOnInit () {
50 this.buildForm({
51 username: this.userValidatorsService.USER_USERNAME,
52 password: this.userValidatorsService.USER_PASSWORD,
53 email: this.userValidatorsService.USER_EMAIL
54 })
55
56 this.serverService.configLoaded
57 .subscribe(() => this.buildQuotaHelpIndication())
58 }
59
60 signup () {
61 this.error = null
62
63 const userCreate: UserCreate = this.form.value
64
65 this.userService.signup(userCreate).subscribe(
66 () => {
67 this.notificationsService.success(
68 this.i18n('Success'),
69 this.i18n('Registration for {{username}} complete.', { username: userCreate.username })
70 )
71 this.redirectService.redirectToHomepage()
72 },
73
74 err => this.error = err.message
75 )
76 }
77
78 private buildQuotaHelpIndication () {
79 if (this.initialUserVideoQuota === -1) return
80
81 const initialUserVideoQuotaBit = this.initialUserVideoQuota * 8
82
83 // 1080p: ~ 6Mbps
84 // 720p: ~ 4Mbps
85 // 360p: ~ 1.5Mbps
86 const fullHdSeconds = initialUserVideoQuotaBit / (6 * 1000 * 1000)
87 const hdSeconds = initialUserVideoQuotaBit / (4 * 1000 * 1000)
88 const normalSeconds = initialUserVideoQuotaBit / (1.5 * 1000 * 1000)
89
90 const lines = [
91 this.i18n('{{seconds}} of full HD videos', { seconds: SignupComponent.getApproximateTime(fullHdSeconds) }),
92 this.i18n('{{seconds}} of HD videos', { seconds: SignupComponent.getApproximateTime(hdSeconds) }),
93 this.i18n('{{seconds}} of average quality videos', { seconds: SignupComponent.getApproximateTime(normalSeconds) })
94 ]
95
96 this.quotaHelpIndication = lines.join('<br />')
97 }
98 }