1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { Router } from '@angular/router'
5 import { NotificationsService } from 'angular2-notifications'
7 import { UserService } from '../shared'
14 } from '../../../shared'
15 import { UserCreate } from '../../../../../../shared'
18 selector: 'my-user-add',
19 templateUrl: './user-add.component.html'
21 export class UserAddComponent extends FormReactive implements OnInit {
31 validationMessages = {
32 'username': USER_USERNAME.MESSAGES,
33 'email': USER_EMAIL.MESSAGES,
34 'password': USER_PASSWORD.MESSAGES,
35 'videoQuota': USER_VIDEO_QUOTA.MESSAGES
39 private formBuilder: FormBuilder,
40 private router: Router,
41 private notificationsService: NotificationsService,
42 private userService: UserService
48 this.form = this.formBuilder.group({
49 username: [ '', USER_USERNAME.VALIDATORS ],
50 email: [ '', USER_EMAIL.VALIDATORS ],
51 password: [ '', USER_PASSWORD.VALIDATORS ],
52 videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
55 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
65 const userCreate: UserCreate = this.form.value
67 // A select in HTML is always mapped as a string, we convert it to number
68 userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
70 this.userService.addUser(userCreate).subscribe(
72 this.notificationsService.success('Success', `User ${userCreate.username} created.`)
73 this.router.navigate([ '/admin/users/list' ])
76 err => this.error = err.text