]>
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' | |
7da18e44 | 4 | |
df98563e | 5 | import { NotificationsService } from 'angular2-notifications' |
7ddd02c9 | 6 | |
df98563e | 7 | import { UserService } from '../shared' |
ad4a8a1c | 8 | import { |
ad4a8a1c C |
9 | USER_USERNAME, |
10 | USER_EMAIL, | |
b0f9f39e C |
11 | USER_PASSWORD, |
12 | USER_VIDEO_QUOTA | |
df98563e | 13 | } from '../../../shared' |
6a84aafd | 14 | import { ServerService } from '../../../core' |
4771e000 | 15 | import { UserCreate } from '../../../../../../shared' |
8094a898 | 16 | import { UserEdit } from './user-edit' |
7da18e44 C |
17 | |
18 | @Component({ | |
19 | selector: 'my-user-add', | |
6a84aafd C |
20 | templateUrl: './user-edit.component.html', |
21 | styleUrls: [ './user-edit.component.scss' ] | |
7da18e44 | 22 | }) |
8094a898 C |
23 | export class UserAddComponent extends UserEdit implements OnInit { |
24 | error: string | |
7da18e44 | 25 | |
df98563e | 26 | form: FormGroup |
4b2f33f3 C |
27 | formErrors = { |
28 | 'username': '', | |
ad4a8a1c | 29 | 'email': '', |
b0f9f39e C |
30 | 'password': '', |
31 | 'videoQuota': '' | |
df98563e | 32 | } |
4b2f33f3 C |
33 | validationMessages = { |
34 | 'username': USER_USERNAME.MESSAGES, | |
ad4a8a1c | 35 | 'email': USER_EMAIL.MESSAGES, |
b0f9f39e C |
36 | 'password': USER_PASSWORD.MESSAGES, |
37 | 'videoQuota': USER_VIDEO_QUOTA.MESSAGES | |
df98563e | 38 | } |
7da18e44 | 39 | |
df98563e | 40 | constructor ( |
6a84aafd | 41 | protected serverService: ServerService, |
4b2f33f3 C |
42 | private formBuilder: FormBuilder, |
43 | private router: Router, | |
7ddd02c9 | 44 | private notificationsService: NotificationsService, |
4b2f33f3 C |
45 | private userService: UserService |
46 | ) { | |
df98563e | 47 | super() |
4b2f33f3 C |
48 | } |
49 | ||
df98563e | 50 | buildForm () { |
4b2f33f3 C |
51 | this.form = this.formBuilder.group({ |
52 | username: [ '', USER_USERNAME.VALIDATORS ], | |
ad4a8a1c | 53 | email: [ '', USER_EMAIL.VALIDATORS ], |
b0f9f39e C |
54 | password: [ '', USER_PASSWORD.VALIDATORS ], |
55 | videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ] | |
df98563e | 56 | }) |
4b2f33f3 | 57 | |
df98563e | 58 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
4b2f33f3 C |
59 | } |
60 | ||
df98563e C |
61 | ngOnInit () { |
62 | this.buildForm() | |
7da18e44 C |
63 | } |
64 | ||
8094a898 C |
65 | formValidated () { |
66 | this.error = undefined | |
7da18e44 | 67 | |
4771e000 | 68 | const userCreate: UserCreate = this.form.value |
4b2f33f3 | 69 | |
b0f9f39e C |
70 | // A select in HTML is always mapped as a string, we convert it to number |
71 | userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
72 | ||
4771e000 | 73 | this.userService.addUser(userCreate).subscribe( |
7ddd02c9 | 74 | () => { |
4771e000 | 75 | this.notificationsService.success('Success', `User ${userCreate.username} created.`) |
df98563e | 76 | this.router.navigate([ '/admin/users/list' ]) |
7ddd02c9 | 77 | }, |
7da18e44 | 78 | |
03b40f24 | 79 | err => this.error = err |
df98563e | 80 | ) |
7da18e44 | 81 | } |
8094a898 C |
82 | |
83 | isCreation () { | |
84 | return true | |
85 | } | |
86 | ||
87 | getFormButtonTitle () { | |
88 | return 'Add user' | |
89 | } | |
7da18e44 | 90 | } |