1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { Router } from '@angular/router'
4 import { NotificationsService } from 'angular2-notifications'
5 import { UserService } from '../shared'
6 import { USER_EMAIL, USER_PASSWORD, USER_ROLE, USER_USERNAME, USER_VIDEO_QUOTA } from '../../../shared'
7 import { ServerService } from '../../../core'
8 import { UserCreate, UserRole } from '../../../../../../shared'
9 import { UserEdit } from './user-edit'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
13 selector: 'my-user-create',
14 templateUrl: './user-edit.component.html',
15 styleUrls: [ './user-edit.component.scss' ]
17 export class UserCreateComponent extends UserEdit implements OnInit {
28 validationMessages = {
29 'username': USER_USERNAME.MESSAGES,
30 'email': USER_EMAIL.MESSAGES,
31 'password': USER_PASSWORD.MESSAGES,
32 'role': USER_ROLE.MESSAGES,
33 'videoQuota': USER_VIDEO_QUOTA.MESSAGES
37 protected serverService: ServerService,
38 private formBuilder: FormBuilder,
39 private router: Router,
40 private notificationsService: NotificationsService,
41 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 role: [ UserRole.USER, USER_ROLE.VALIDATORS ],
53 videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
56 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
64 this.error = undefined
66 const userCreate: UserCreate = this.form.value
68 // A select in HTML is always mapped as a string, we convert it to number
69 userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
71 this.userService.addUser(userCreate).subscribe(
73 this.notificationsService.success(
75 this.i18n('User {{username}} created.', { username: userCreate.username })
77 this.router.navigate([ '/admin/users/list' ])
80 err => this.error = err.message
88 getFormButtonTitle () {
89 return this.i18n('Create user')