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 { ServerService } from '../../../core'
16 import { UserCreate, UserRole } from '../../../../../../shared'
17 import { UserEdit } from './user-edit'
20 selector: 'my-user-add',
21 templateUrl: './user-edit.component.html',
22 styleUrls: [ './user-edit.component.scss' ]
24 export class UserAddComponent extends UserEdit implements OnInit {
35 validationMessages = {
36 'username': USER_USERNAME.MESSAGES,
37 'email': USER_EMAIL.MESSAGES,
38 'password': USER_PASSWORD.MESSAGES,
39 'role': USER_ROLE.MESSAGES,
40 'videoQuota': USER_VIDEO_QUOTA.MESSAGES
44 protected serverService: ServerService,
45 private formBuilder: FormBuilder,
46 private router: Router,
47 private notificationsService: NotificationsService,
48 private userService: UserService
54 this.form = this.formBuilder.group({
55 username: [ '', USER_USERNAME.VALIDATORS ],
56 email: [ '', USER_EMAIL.VALIDATORS ],
57 password: [ '', USER_PASSWORD.VALIDATORS ],
58 role: [ UserRole.USER, USER_ROLE.VALIDATORS ],
59 videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
62 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
70 this.error = undefined
72 const userCreate: UserCreate = this.form.value
74 // A select in HTML is always mapped as a string, we convert it to number
75 userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
77 this.userService.addUser(userCreate).subscribe(
79 this.notificationsService.success('Success', `User ${userCreate.username} created.`)
80 this.router.navigate([ '/admin/users/list' ])
83 err => this.error = err.message
91 getFormButtonTitle () {