]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-edit/user-add.component.ts
Take in account transcoding for video quota
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / 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 USER_USERNAME,
10 USER_EMAIL,
11 USER_PASSWORD,
12 USER_VIDEO_QUOTA
13 } from '../../../shared'
14 import { ServerService } from '../../../core'
15 import { UserCreate } from '../../../../../../shared'
16 import { UserEdit } from './user-edit'
17
18 @Component({
19 selector: 'my-user-add',
20 templateUrl: './user-edit.component.html',
21 styleUrls: [ './user-edit.component.scss' ]
22 })
23 export class UserAddComponent extends UserEdit implements OnInit {
24 error: string
25
26 form: FormGroup
27 formErrors = {
28 'username': '',
29 'email': '',
30 'password': '',
31 'videoQuota': ''
32 }
33 validationMessages = {
34 'username': USER_USERNAME.MESSAGES,
35 'email': USER_EMAIL.MESSAGES,
36 'password': USER_PASSWORD.MESSAGES,
37 'videoQuota': USER_VIDEO_QUOTA.MESSAGES
38 }
39
40 constructor (
41 protected serverService: ServerService,
42 private formBuilder: FormBuilder,
43 private router: Router,
44 private notificationsService: NotificationsService,
45 private userService: UserService
46 ) {
47 super()
48 }
49
50 buildForm () {
51 this.form = this.formBuilder.group({
52 username: [ '', USER_USERNAME.VALIDATORS ],
53 email: [ '', USER_EMAIL.VALIDATORS ],
54 password: [ '', USER_PASSWORD.VALIDATORS ],
55 videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
56 })
57
58 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
59 }
60
61 ngOnInit () {
62 this.buildForm()
63 }
64
65 formValidated () {
66 this.error = undefined
67
68 const userCreate: UserCreate = this.form.value
69
70 // A select in HTML is always mapped as a string, we convert it to number
71 userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
72
73 this.userService.addUser(userCreate).subscribe(
74 () => {
75 this.notificationsService.success('Success', `User ${userCreate.username} created.`)
76 this.router.navigate([ '/admin/users/list' ])
77 },
78
79 err => this.error = err
80 )
81 }
82
83 isCreation () {
84 return true
85 }
86
87 getFormButtonTitle () {
88 return 'Add user'
89 }
90 }