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