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