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