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'
11 selector: 'my-signup',
12 templateUrl: './signup.component.html',
13 styleUrls: [ './signup.component.scss' ]
15 export class SignupComponent extends FormReactive implements OnInit {
17 quotaHelpIndication = ''
25 validationMessages = {
26 'username': USER_USERNAME.MESSAGES,
27 'email': USER_EMAIL.MESSAGES,
28 'password': USER_PASSWORD.MESSAGES
31 private static getApproximateTime (seconds: number) {
32 const hours = Math.floor(seconds / 3600)
34 if (hours > 1) pluralSuffix = 's'
35 if (hours > 0) return `~ ${hours} hour${pluralSuffix}`
37 const minutes = Math.floor(seconds % 3600 / 60)
38 if (minutes > 1) pluralSuffix = 's'
40 return `~ ${minutes} minute${pluralSuffix}`
44 private formBuilder: FormBuilder,
45 private router: Router,
46 private notificationsService: NotificationsService,
47 private userService: UserService,
48 private serverService: ServerService
53 get initialUserVideoQuota () {
54 return this.serverService.getConfig().user.videoQuota
58 this.form = this.formBuilder.group({
59 username: [ '', USER_USERNAME.VALIDATORS ],
60 email: [ '', USER_EMAIL.VALIDATORS ],
61 password: [ '', USER_PASSWORD.VALIDATORS ]
64 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
70 this.serverService.configLoaded
71 .subscribe(() => this.buildQuotaHelpIndication())
77 const userCreate: UserCreate = this.form.value
79 this.userService.signup(userCreate).subscribe(
81 this.notificationsService.success('Success', `Registration for ${userCreate.username} complete.`)
82 this.router.navigate([ '/videos/list' ])
85 err => this.error = err.message
89 private buildQuotaHelpIndication () {
90 if (this.initialUserVideoQuota === -1) return
92 const initialUserVideoQuotaBit = this.initialUserVideoQuota * 8
97 const fullHdSeconds = initialUserVideoQuotaBit / (6 * 1000 * 1000)
98 const hdSeconds = initialUserVideoQuotaBit / (4 * 1000 * 1000)
99 const normalSeconds = initialUserVideoQuotaBit / (1.5 * 1000 * 1000)
102 SignupComponent.getApproximateTime(fullHdSeconds) + ' of full HD videos',
103 SignupComponent.getApproximateTime(hdSeconds) + ' of HD videos',
104 SignupComponent.getApproximateTime(normalSeconds) + ' of average quality videos'
107 this.quotaHelpIndication = lines.join('<br />')