1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { Router } from '@angular/router'
4 import { ServerService } from '@app/core/server'
6 import { NotificationsService } from 'angular2-notifications'
7 import { UserCreate } from '../../../../shared'
8 import { FormReactive, USER_EMAIL, USER_PASSWORD, USER_USERNAME, UserService } from '../shared'
9 import { RedirectService } from '@app/core'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
13 selector: 'my-signup',
14 templateUrl: './signup.component.html',
15 styleUrls: [ './signup.component.scss' ]
17 export class SignupComponent extends FormReactive implements OnInit {
19 quotaHelpIndication = ''
27 validationMessages = {
28 'username': USER_USERNAME.MESSAGES,
29 'email': USER_EMAIL.MESSAGES,
30 'password': USER_PASSWORD.MESSAGES
33 private static getApproximateTime (seconds: number) {
34 const hours = Math.floor(seconds / 3600)
36 if (hours > 1) pluralSuffix = 's'
37 if (hours > 0) return `~ ${hours} hour${pluralSuffix}`
39 const minutes = Math.floor(seconds % 3600 / 60)
40 if (minutes > 1) pluralSuffix = 's'
42 return `~ ${minutes} minute${pluralSuffix}`
46 private formBuilder: FormBuilder,
47 private router: Router,
48 private notificationsService: NotificationsService,
49 private userService: UserService,
50 private redirectService: RedirectService,
51 private serverService: ServerService,
57 get initialUserVideoQuota () {
58 return this.serverService.getConfig().user.videoQuota
62 this.form = this.formBuilder.group({
63 username: [ '', USER_USERNAME.VALIDATORS ],
64 email: [ '', USER_EMAIL.VALIDATORS ],
65 password: [ '', USER_PASSWORD.VALIDATORS ]
68 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
74 this.serverService.configLoaded
75 .subscribe(() => this.buildQuotaHelpIndication())
81 const userCreate: UserCreate = this.form.value
83 this.userService.signup(userCreate).subscribe(
85 this.notificationsService.success(
87 this.i18n('Registration for {{ username }} complete.', { username: userCreate.username })
89 this.redirectService.redirectToHomepage()
92 err => this.error = err.message
96 private buildQuotaHelpIndication () {
97 if (this.initialUserVideoQuota === -1) return
99 const initialUserVideoQuotaBit = this.initialUserVideoQuota * 8
104 const fullHdSeconds = initialUserVideoQuotaBit / (6 * 1000 * 1000)
105 const hdSeconds = initialUserVideoQuotaBit / (4 * 1000 * 1000)
106 const normalSeconds = initialUserVideoQuotaBit / (1.5 * 1000 * 1000)
109 this.i18n('{{ seconds }} of full HD videos', { seconds: SignupComponent.getApproximateTime(fullHdSeconds) }),
110 this.i18n('{{ seconds }} of HD videos', { seconds: SignupComponent.getApproximateTime(hdSeconds) }),
111 this.i18n('{{ seconds }} of average quality videos', { seconds: SignupComponent.getApproximateTime(normalSeconds) })
114 this.quotaHelpIndication = lines.join('<br />')