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