]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-edit/user-create.component.ts
Form validators refractoring
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-create.component.ts
CommitLineData
df98563e 1import { Component, OnInit } from '@angular/core'
df98563e 2import { Router } from '@angular/router'
df98563e 3import { NotificationsService } from 'angular2-notifications'
df98563e 4import { UserService } from '../shared'
b1d40cff 5import { USER_EMAIL, USER_PASSWORD, USER_ROLE, USER_USERNAME, USER_VIDEO_QUOTA } from '../../../shared'
6a84aafd 6import { ServerService } from '../../../core'
954605a8 7import { UserCreate, UserRole } from '../../../../../../shared'
8094a898 8import { UserEdit } from './user-edit'
b1d40cff 9import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 10import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
7da18e44
C
11
12@Component({
4c200caa 13 selector: 'my-user-create',
6a84aafd
C
14 templateUrl: './user-edit.component.html',
15 styleUrls: [ './user-edit.component.scss' ]
7da18e44 16})
4c200caa 17export class UserCreateComponent extends UserEdit implements OnInit {
8094a898 18 error: string
7da18e44 19
df98563e 20 constructor (
6a84aafd 21 protected serverService: ServerService,
d18d6478 22 protected formValidatorService: FormValidatorService,
4b2f33f3 23 private router: Router,
7ddd02c9 24 private notificationsService: NotificationsService,
b1d40cff
C
25 private userService: UserService,
26 private i18n: I18n
4b2f33f3 27 ) {
df98563e 28 super()
4b2f33f3
C
29 }
30
df98563e 31 ngOnInit () {
d18d6478
C
32 const defaultValues = {
33 role: UserRole.USER.toString(),
34 videoQuota: '-1'
35 }
36
37 this.buildForm({
38 username: USER_USERNAME,
39 email: USER_EMAIL,
40 password: USER_PASSWORD,
41 role: USER_ROLE,
42 videoQuota: USER_VIDEO_QUOTA
43 }, defaultValues)
7da18e44
C
44 }
45
8094a898
C
46 formValidated () {
47 this.error = undefined
7da18e44 48
4771e000 49 const userCreate: UserCreate = this.form.value
4b2f33f3 50
b0f9f39e
C
51 // A select in HTML is always mapped as a string, we convert it to number
52 userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
53
4771e000 54 this.userService.addUser(userCreate).subscribe(
7ddd02c9 55 () => {
b1d40cff
C
56 this.notificationsService.success(
57 this.i18n('Success'),
25acef90 58 this.i18n('User {{username}} created.', { username: userCreate.username })
b1d40cff 59 )
df98563e 60 this.router.navigate([ '/admin/users/list' ])
7ddd02c9 61 },
7da18e44 62
f7354483 63 err => this.error = err.message
df98563e 64 )
7da18e44 65 }
8094a898
C
66
67 isCreation () {
68 return true
69 }
70
71 getFormButtonTitle () {
b1d40cff 72 return this.i18n('Create user')
8094a898 73 }
7da18e44 74}