]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit } from '@angular/core' | |
2 | import { FormBuilder, FormGroup } from '@angular/forms' | |
3 | import { Router } from '@angular/router' | |
4 | import { NotificationsService } from 'angular2-notifications' | |
5 | import { UserService } from '../shared' | |
6 | import { USER_EMAIL, USER_PASSWORD, USER_ROLE, USER_USERNAME, USER_VIDEO_QUOTA } from '../../../shared' | |
7 | import { ServerService } from '../../../core' | |
8 | import { UserCreate, UserRole } from '../../../../../../shared' | |
9 | import { UserEdit } from './user-edit' | |
10 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
11 | ||
12 | @Component({ | |
13 | selector: 'my-user-create', | |
14 | templateUrl: './user-edit.component.html', | |
15 | styleUrls: [ './user-edit.component.scss' ] | |
16 | }) | |
17 | export class UserCreateComponent extends UserEdit implements OnInit { | |
18 | error: string | |
19 | ||
20 | form: FormGroup | |
21 | formErrors = { | |
22 | 'username': '', | |
23 | 'email': '', | |
24 | 'password': '', | |
25 | 'role': '', | |
26 | 'videoQuota': '' | |
27 | } | |
28 | validationMessages = { | |
29 | 'username': USER_USERNAME.MESSAGES, | |
30 | 'email': USER_EMAIL.MESSAGES, | |
31 | 'password': USER_PASSWORD.MESSAGES, | |
32 | 'role': USER_ROLE.MESSAGES, | |
33 | 'videoQuota': USER_VIDEO_QUOTA.MESSAGES | |
34 | } | |
35 | ||
36 | constructor ( | |
37 | protected serverService: ServerService, | |
38 | private formBuilder: FormBuilder, | |
39 | private router: Router, | |
40 | private notificationsService: NotificationsService, | |
41 | private userService: UserService, | |
42 | private i18n: I18n | |
43 | ) { | |
44 | super() | |
45 | } | |
46 | ||
47 | buildForm () { | |
48 | this.form = this.formBuilder.group({ | |
49 | username: [ '', USER_USERNAME.VALIDATORS ], | |
50 | email: [ '', USER_EMAIL.VALIDATORS ], | |
51 | password: [ '', USER_PASSWORD.VALIDATORS ], | |
52 | role: [ UserRole.USER, USER_ROLE.VALIDATORS ], | |
53 | videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ] | |
54 | }) | |
55 | ||
56 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
57 | } | |
58 | ||
59 | ngOnInit () { | |
60 | this.buildForm() | |
61 | } | |
62 | ||
63 | formValidated () { | |
64 | this.error = undefined | |
65 | ||
66 | const userCreate: UserCreate = this.form.value | |
67 | ||
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 | ||
71 | this.userService.addUser(userCreate).subscribe( | |
72 | () => { | |
73 | this.notificationsService.success( | |
74 | this.i18n('Success'), | |
75 | this.i18n('User {{ username }} created.', { username: userCreate.username }) | |
76 | ) | |
77 | this.router.navigate([ '/admin/users/list' ]) | |
78 | }, | |
79 | ||
80 | err => this.error = err.message | |
81 | ) | |
82 | } | |
83 | ||
84 | isCreation () { | |
85 | return true | |
86 | } | |
87 | ||
88 | getFormButtonTitle () { | |
89 | return this.i18n('Create user') | |
90 | } | |
91 | } |