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