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