]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-edit/user-add.component.ts
Fix error messages on client forms
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / 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 8import {
ad4a8a1c
C
9 USER_USERNAME,
10 USER_EMAIL,
b0f9f39e 11 USER_PASSWORD,
954605a8
C
12 USER_VIDEO_QUOTA,
13 USER_ROLE
df98563e 14} from '../../../shared'
6a84aafd 15import { ServerService } from '../../../core'
954605a8 16import { UserCreate, UserRole } from '../../../../../../shared'
8094a898 17import { UserEdit } from './user-edit'
7da18e44
C
18
19@Component({
20 selector: 'my-user-add',
6a84aafd
C
21 templateUrl: './user-edit.component.html',
22 styleUrls: [ './user-edit.component.scss' ]
7da18e44 23})
8094a898
C
24export class UserAddComponent extends UserEdit implements OnInit {
25 error: string
7da18e44 26
df98563e 27 form: FormGroup
4b2f33f3
C
28 formErrors = {
29 'username': '',
ad4a8a1c 30 'email': '',
b0f9f39e 31 'password': '',
954605a8 32 'role': '',
b0f9f39e 33 'videoQuota': ''
df98563e 34 }
4b2f33f3
C
35 validationMessages = {
36 'username': USER_USERNAME.MESSAGES,
ad4a8a1c 37 'email': USER_EMAIL.MESSAGES,
b0f9f39e 38 'password': USER_PASSWORD.MESSAGES,
954605a8 39 'role': USER_ROLE.MESSAGES,
b0f9f39e 40 'videoQuota': USER_VIDEO_QUOTA.MESSAGES
df98563e 41 }
7da18e44 42
df98563e 43 constructor (
6a84aafd 44 protected serverService: ServerService,
4b2f33f3
C
45 private formBuilder: FormBuilder,
46 private router: Router,
7ddd02c9 47 private notificationsService: NotificationsService,
4b2f33f3
C
48 private userService: UserService
49 ) {
df98563e 50 super()
4b2f33f3
C
51 }
52
df98563e 53 buildForm () {
4b2f33f3
C
54 this.form = this.formBuilder.group({
55 username: [ '', USER_USERNAME.VALIDATORS ],
ad4a8a1c 56 email: [ '', USER_EMAIL.VALIDATORS ],
b0f9f39e 57 password: [ '', USER_PASSWORD.VALIDATORS ],
954605a8 58 role: [ UserRole.USER, USER_ROLE.VALIDATORS ],
b0f9f39e 59 videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
df98563e 60 })
4b2f33f3 61
df98563e 62 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
4b2f33f3
C
63 }
64
df98563e
C
65 ngOnInit () {
66 this.buildForm()
7da18e44
C
67 }
68
8094a898
C
69 formValidated () {
70 this.error = undefined
7da18e44 71
4771e000 72 const userCreate: UserCreate = this.form.value
4b2f33f3 73
b0f9f39e
C
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)
76
4771e000 77 this.userService.addUser(userCreate).subscribe(
7ddd02c9 78 () => {
4771e000 79 this.notificationsService.success('Success', `User ${userCreate.username} created.`)
df98563e 80 this.router.navigate([ '/admin/users/list' ])
7ddd02c9 81 },
7da18e44 82
f7354483 83 err => this.error = err.message
df98563e 84 )
7da18e44 85 }
8094a898
C
86
87 isCreation () {
88 return true
89 }
90
91 getFormButtonTitle () {
92 return 'Add user'
93 }
7da18e44 94}