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