]>
Commit | Line | Data |
---|---|---|
df98563e | 1 | import { Component, OnInit } from '@angular/core' |
5afdd0a5 | 2 | import { FormBuilder, FormGroup } from '@angular/forms' |
df98563e | 3 | import { Router } from '@angular/router' |
5afdd0a5 | 4 | import { ServerService } from '@app/core/server' |
a184c71b | 5 | |
df98563e | 6 | import { NotificationsService } from 'angular2-notifications' |
4771e000 | 7 | import { UserCreate } from '../../../../shared' |
5afdd0a5 | 8 | import { FormReactive, USER_EMAIL, USER_PASSWORD, USER_USERNAME, UserService } from '../shared' |
a184c71b C |
9 | |
10 | @Component({ | |
11 | selector: 'my-signup', | |
d235f6b0 C |
12 | templateUrl: './signup.component.html', |
13 | styleUrls: [ './signup.component.scss' ] | |
a184c71b C |
14 | }) |
15 | export class SignupComponent extends FormReactive implements OnInit { | |
df98563e | 16 | error: string = null |
5afdd0a5 | 17 | quotaHelpIndication = '' |
a184c71b | 18 | |
df98563e | 19 | form: FormGroup |
a184c71b C |
20 | formErrors = { |
21 | 'username': '', | |
22 | 'email': '', | |
23 | 'password': '' | |
df98563e | 24 | } |
a184c71b C |
25 | validationMessages = { |
26 | 'username': USER_USERNAME.MESSAGES, | |
27 | 'email': USER_EMAIL.MESSAGES, | |
df98563e C |
28 | 'password': USER_PASSWORD.MESSAGES |
29 | } | |
a184c71b | 30 | |
5afdd0a5 C |
31 | private static getApproximateTime (seconds: number) { |
32 | const hours = Math.floor(seconds / 3600) | |
33 | let pluralSuffix = '' | |
34 | if (hours > 1) pluralSuffix = 's' | |
35 | if (hours > 0) return `~ ${hours} hour${pluralSuffix}` | |
36 | ||
37 | const minutes = Math.floor(seconds % 3600 / 60) | |
38 | if (minutes > 1) pluralSuffix = 's' | |
39 | ||
40 | return `~ ${minutes} minute${pluralSuffix}` | |
41 | } | |
42 | ||
df98563e | 43 | constructor ( |
a184c71b C |
44 | private formBuilder: FormBuilder, |
45 | private router: Router, | |
46 | private notificationsService: NotificationsService, | |
5afdd0a5 C |
47 | private userService: UserService, |
48 | private serverService: ServerService | |
a184c71b | 49 | ) { |
df98563e | 50 | super() |
a184c71b C |
51 | } |
52 | ||
5afdd0a5 C |
53 | get initialUserVideoQuota () { |
54 | return this.serverService.getConfig().user.videoQuota | |
55 | } | |
56 | ||
df98563e | 57 | buildForm () { |
a184c71b C |
58 | this.form = this.formBuilder.group({ |
59 | username: [ '', USER_USERNAME.VALIDATORS ], | |
60 | email: [ '', USER_EMAIL.VALIDATORS ], | |
df98563e C |
61 | password: [ '', USER_PASSWORD.VALIDATORS ] |
62 | }) | |
a184c71b | 63 | |
df98563e | 64 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
a184c71b C |
65 | } |
66 | ||
df98563e C |
67 | ngOnInit () { |
68 | this.buildForm() | |
5afdd0a5 C |
69 | |
70 | this.serverService.configLoaded | |
71 | .subscribe(() => this.buildQuotaHelpIndication()) | |
a184c71b C |
72 | } |
73 | ||
df98563e C |
74 | signup () { |
75 | this.error = null | |
a184c71b | 76 | |
4771e000 | 77 | const userCreate: UserCreate = this.form.value |
a184c71b | 78 | |
4771e000 | 79 | this.userService.signup(userCreate).subscribe( |
a184c71b | 80 | () => { |
4771e000 | 81 | this.notificationsService.success('Success', `Registration for ${userCreate.username} complete.`) |
df98563e | 82 | this.router.navigate([ '/videos/list' ]) |
a184c71b C |
83 | }, |
84 | ||
f7354483 | 85 | err => this.error = err.message |
df98563e | 86 | ) |
a184c71b | 87 | } |
5afdd0a5 C |
88 | |
89 | private buildQuotaHelpIndication () { | |
90 | if (this.initialUserVideoQuota === -1) return | |
91 | ||
92 | const initialUserVideoQuotaBit = this.initialUserVideoQuota * 8 | |
93 | ||
94 | // 1080p: ~ 6Mbps | |
95 | // 720p: ~ 4Mbps | |
96 | // 360p: ~ 1.5Mbps | |
97 | const fullHdSeconds = initialUserVideoQuotaBit / (6 * 1000 * 1000) | |
98 | const hdSeconds = initialUserVideoQuotaBit / (4 * 1000 * 1000) | |
99 | const normalSeconds = initialUserVideoQuotaBit / (1.5 * 1000 * 1000) | |
100 | ||
101 | const lines = [ | |
102 | SignupComponent.getApproximateTime(fullHdSeconds) + ' of full HD videos', | |
103 | SignupComponent.getApproximateTime(hdSeconds) + ' of HD videos', | |
490b595a | 104 | SignupComponent.getApproximateTime(normalSeconds) + ' of average quality videos' |
5afdd0a5 C |
105 | ] |
106 | ||
107 | this.quotaHelpIndication = lines.join('<br />') | |
108 | } | |
a184c71b | 109 | } |