]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-add/user-add.component.ts
Better typescript typing for a better world
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-add / user-add.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { Router } from '@angular/router'
4
5 import { NotificationsService } from 'angular2-notifications'
6
7 import { UserService } from '../shared'
8 import {
9 FormReactive,
10 USER_USERNAME,
11 USER_EMAIL,
12 USER_PASSWORD
13 } from '../../../shared'
14 import { UserCreate } from '../../../../../../shared'
15
16 @Component({
17 selector: 'my-user-add',
18 templateUrl: './user-add.component.html'
19 })
20 export class UserAddComponent extends FormReactive implements OnInit {
21 error: string = null
22
23 form: FormGroup
24 formErrors = {
25 'username': '',
26 'email': '',
27 'password': ''
28 }
29 validationMessages = {
30 'username': USER_USERNAME.MESSAGES,
31 'email': USER_EMAIL.MESSAGES,
32 'password': USER_PASSWORD.MESSAGES
33 }
34
35 constructor (
36 private formBuilder: FormBuilder,
37 private router: Router,
38 private notificationsService: NotificationsService,
39 private userService: UserService
40 ) {
41 super()
42 }
43
44 buildForm () {
45 this.form = this.formBuilder.group({
46 username: [ '', USER_USERNAME.VALIDATORS ],
47 email: [ '', USER_EMAIL.VALIDATORS ],
48 password: [ '', USER_PASSWORD.VALIDATORS ]
49 })
50
51 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
52 }
53
54 ngOnInit () {
55 this.buildForm()
56 }
57
58 addUser () {
59 this.error = null
60
61 const userCreate: UserCreate = this.form.value
62
63 this.userService.addUser(userCreate).subscribe(
64 () => {
65 this.notificationsService.success('Success', `User ${userCreate.username} created.`)
66 this.router.navigate([ '/admin/users/list' ])
67 },
68
69 err => this.error = err.text
70 )
71 }
72 }