]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Component, OnInit } from '@angular/core' |
2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms' | |
3 | import { Router } from '@angular/router' | |
a184c71b | 4 | |
df98563e | 5 | import { NotificationsService } from 'angular2-notifications' |
a184c71b | 6 | |
df98563e | 7 | import { AuthService } from '../core' |
a184c71b C |
8 | import { |
9 | FormReactive, | |
10 | UserService, | |
11 | USER_USERNAME, | |
12 | USER_EMAIL, | |
13 | USER_PASSWORD | |
df98563e | 14 | } from '../shared' |
a184c71b C |
15 | |
16 | @Component({ | |
17 | selector: 'my-signup', | |
18 | templateUrl: './signup.component.html' | |
19 | }) | |
20 | export class SignupComponent extends FormReactive implements OnInit { | |
df98563e | 21 | error: string = null |
a184c71b | 22 | |
df98563e | 23 | form: FormGroup |
a184c71b C |
24 | formErrors = { |
25 | 'username': '', | |
26 | 'email': '', | |
27 | 'password': '' | |
df98563e | 28 | } |
a184c71b C |
29 | validationMessages = { |
30 | 'username': USER_USERNAME.MESSAGES, | |
31 | 'email': USER_EMAIL.MESSAGES, | |
df98563e C |
32 | 'password': USER_PASSWORD.MESSAGES |
33 | } | |
a184c71b | 34 | |
df98563e | 35 | constructor ( |
a184c71b C |
36 | private formBuilder: FormBuilder, |
37 | private router: Router, | |
38 | private notificationsService: NotificationsService, | |
39 | private userService: UserService | |
40 | ) { | |
df98563e | 41 | super() |
a184c71b C |
42 | } |
43 | ||
df98563e | 44 | buildForm () { |
a184c71b C |
45 | this.form = this.formBuilder.group({ |
46 | username: [ '', USER_USERNAME.VALIDATORS ], | |
47 | email: [ '', USER_EMAIL.VALIDATORS ], | |
df98563e C |
48 | password: [ '', USER_PASSWORD.VALIDATORS ] |
49 | }) | |
a184c71b | 50 | |
df98563e | 51 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
a184c71b C |
52 | } |
53 | ||
df98563e C |
54 | ngOnInit () { |
55 | this.buildForm() | |
a184c71b C |
56 | } |
57 | ||
df98563e C |
58 | signup () { |
59 | this.error = null | |
a184c71b | 60 | |
df98563e | 61 | const { username, password, email } = this.form.value |
a184c71b C |
62 | |
63 | this.userService.signup(username, password, email).subscribe( | |
64 | () => { | |
df98563e C |
65 | this.notificationsService.success('Success', `Registration for ${username} complete.`) |
66 | this.router.navigate([ '/videos/list' ]) | |
a184c71b C |
67 | }, |
68 | ||
69 | err => this.error = err.text | |
df98563e | 70 | ) |
a184c71b C |
71 | } |
72 | } |