]>
Commit | Line | Data |
---|---|---|
df98563e | 1 | import { Component, OnInit } from '@angular/core' |
df98563e | 2 | import { Router } from '@angular/router' |
df98563e | 3 | import { NotificationsService } from 'angular2-notifications' |
df98563e | 4 | import { UserService } from '../shared' |
6a84aafd | 5 | import { ServerService } from '../../../core' |
954605a8 | 6 | import { UserCreate, UserRole } from '../../../../../../shared' |
8094a898 | 7 | import { UserEdit } from './user-edit' |
b1d40cff | 8 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 9 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 10 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' |
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, |
e309822b | 23 | private userValidatorsService: UserValidatorsService, |
4b2f33f3 | 24 | private router: Router, |
7ddd02c9 | 25 | private notificationsService: NotificationsService, |
b1d40cff C |
26 | private userService: UserService, |
27 | private i18n: I18n | |
4b2f33f3 | 28 | ) { |
df98563e | 29 | super() |
4b2f33f3 C |
30 | } |
31 | ||
df98563e | 32 | ngOnInit () { |
d18d6478 C |
33 | const defaultValues = { |
34 | role: UserRole.USER.toString(), | |
35 | videoQuota: '-1' | |
36 | } | |
37 | ||
38 | this.buildForm({ | |
e309822b C |
39 | username: this.userValidatorsService.USER_USERNAME, |
40 | email: this.userValidatorsService.USER_EMAIL, | |
41 | password: this.userValidatorsService.USER_PASSWORD, | |
42 | role: this.userValidatorsService.USER_ROLE, | |
43 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA | |
d18d6478 | 44 | }, defaultValues) |
7da18e44 C |
45 | } |
46 | ||
8094a898 C |
47 | formValidated () { |
48 | this.error = undefined | |
7da18e44 | 49 | |
4771e000 | 50 | const userCreate: UserCreate = this.form.value |
4b2f33f3 | 51 | |
b0f9f39e C |
52 | // A select in HTML is always mapped as a string, we convert it to number |
53 | userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
54 | ||
4771e000 | 55 | this.userService.addUser(userCreate).subscribe( |
7ddd02c9 | 56 | () => { |
b1d40cff C |
57 | this.notificationsService.success( |
58 | this.i18n('Success'), | |
25acef90 | 59 | this.i18n('User {{username}} created.', { username: userCreate.username }) |
b1d40cff | 60 | ) |
df98563e | 61 | this.router.navigate([ '/admin/users/list' ]) |
7ddd02c9 | 62 | }, |
7da18e44 | 63 | |
f7354483 | 64 | err => this.error = err.message |
df98563e | 65 | ) |
7da18e44 | 66 | } |
8094a898 C |
67 | |
68 | isCreation () { | |
69 | return true | |
70 | } | |
71 | ||
72 | getFormButtonTitle () { | |
b1d40cff | 73 | return this.i18n('Create user') |
8094a898 | 74 | } |
7da18e44 | 75 | } |