]>
Commit | Line | Data |
---|---|---|
df98563e | 1 | import { Component, OnInit } from '@angular/core' |
df98563e | 2 | import { Router } from '@angular/router' |
a95a4cc8 | 3 | import { AuthService, Notifier, ServerService } from '@app/core' |
954605a8 | 4 | import { UserCreate, UserRole } from '../../../../../../shared' |
8094a898 | 5 | import { UserEdit } from './user-edit' |
b1d40cff | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 7 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 8 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' |
3827c3b3 | 9 | import { ConfigService } from '@app/+admin/config/shared/config.service' |
e724fa93 | 10 | import { UserService } from '@app/shared' |
7da18e44 C |
11 | |
12 | @Component({ | |
4c200caa | 13 | selector: 'my-user-create', |
6a84aafd C |
14 | templateUrl: './user-edit.component.html', |
15 | styleUrls: [ './user-edit.component.scss' ] | |
7da18e44 | 16 | }) |
4c200caa | 17 | export class UserCreateComponent extends UserEdit implements OnInit { |
8094a898 | 18 | error: string |
7da18e44 | 19 | |
df98563e | 20 | constructor ( |
6a84aafd | 21 | protected serverService: ServerService, |
d18d6478 | 22 | protected formValidatorService: FormValidatorService, |
3827c3b3 | 23 | protected configService: ConfigService, |
a95a4cc8 | 24 | protected auth: AuthService, |
e309822b | 25 | private userValidatorsService: UserValidatorsService, |
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, | |
48 | password: this.userValidatorsService.USER_PASSWORD, | |
49 | role: this.userValidatorsService.USER_ROLE, | |
d6ca951b | 50 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, |
1eddc9a7 C |
51 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY, |
52 | byPassAutoBlacklist: 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 | ||
81 | getFormButtonTitle () { | |
b1d40cff | 82 | return this.i18n('Create user') |
8094a898 | 83 | } |
7da18e44 | 84 | } |