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