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