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