]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-add/user-add.component.ts
Use typescript standard and lint all files
[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
15 @Component({
16 selector: 'my-user-add',
17 templateUrl: './user-add.component.html'
18 })
19 export class UserAddComponent extends FormReactive implements OnInit {
20 error: string = null
21
22 form: FormGroup
23 formErrors = {
24 'username': '',
25 'email': '',
26 'password': ''
27 }
28 validationMessages = {
29 'username': USER_USERNAME.MESSAGES,
30 'email': USER_EMAIL.MESSAGES,
31 'password': USER_PASSWORD.MESSAGES
32 }
33
34 constructor (
35 private formBuilder: FormBuilder,
36 private router: Router,
37 private notificationsService: NotificationsService,
38 private userService: UserService
39 ) {
40 super()
41 }
42
43 buildForm () {
44 this.form = this.formBuilder.group({
45 username: [ '', USER_USERNAME.VALIDATORS ],
46 email: [ '', USER_EMAIL.VALIDATORS ],
47 password: [ '', USER_PASSWORD.VALIDATORS ]
48 })
49
50 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
51 }
52
53 ngOnInit () {
54 this.buildForm()
55 }
56
57 addUser () {
58 this.error = null
59
60 const { username, password, email } = this.form.value
61
62 this.userService.addUser(username, password, email).subscribe(
63 () => {
64 this.notificationsService.success('Success', `User ${username} created.`)
65 this.router.navigate([ '/admin/users/list' ])
66 },
67
68 err => this.error = err.text
69 )
70 }
71 }