2017-06-16 14:32:15 +02:00
|
|
|
import { Component, OnInit } from '@angular/core'
|
|
|
|
import { FormBuilder, FormGroup } from '@angular/forms'
|
|
|
|
import { Router } from '@angular/router'
|
|
|
|
import { NotificationsService } from 'angular2-notifications'
|
|
|
|
import { UserService } from '../shared'
|
2018-06-04 16:21:17 +02:00
|
|
|
import { USER_EMAIL, USER_PASSWORD, USER_ROLE, USER_USERNAME, USER_VIDEO_QUOTA } from '../../../shared'
|
2017-10-19 17:33:32 +02:00
|
|
|
import { ServerService } from '../../../core'
|
2017-10-27 16:55:03 +02:00
|
|
|
import { UserCreate, UserRole } from '../../../../../../shared'
|
2017-09-05 21:29:39 +02:00
|
|
|
import { UserEdit } from './user-edit'
|
2018-06-04 16:21:17 +02:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
2016-08-09 21:45:21 +02:00
|
|
|
|
|
|
|
@Component({
|
2018-05-09 15:30:37 +02:00
|
|
|
selector: 'my-user-create',
|
2017-10-19 17:33:32 +02:00
|
|
|
templateUrl: './user-edit.component.html',
|
|
|
|
styleUrls: [ './user-edit.component.scss' ]
|
2016-08-09 21:45:21 +02:00
|
|
|
})
|
2018-05-09 15:30:37 +02:00
|
|
|
export class UserCreateComponent extends UserEdit implements OnInit {
|
2017-09-05 21:29:39 +02:00
|
|
|
error: string
|
2016-08-09 21:45:21 +02:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
form: FormGroup
|
2016-09-09 22:16:51 +02:00
|
|
|
formErrors = {
|
|
|
|
'username': '',
|
2017-02-18 09:29:59 +01:00
|
|
|
'email': '',
|
2017-09-04 20:07:54 +02:00
|
|
|
'password': '',
|
2017-10-27 16:55:03 +02:00
|
|
|
'role': '',
|
2017-09-04 20:07:54 +02:00
|
|
|
'videoQuota': ''
|
2017-06-16 14:32:15 +02:00
|
|
|
}
|
2016-09-09 22:16:51 +02:00
|
|
|
validationMessages = {
|
|
|
|
'username': USER_USERNAME.MESSAGES,
|
2017-02-18 09:29:59 +01:00
|
|
|
'email': USER_EMAIL.MESSAGES,
|
2017-09-04 20:07:54 +02:00
|
|
|
'password': USER_PASSWORD.MESSAGES,
|
2017-10-27 16:55:03 +02:00
|
|
|
'role': USER_ROLE.MESSAGES,
|
2017-09-04 20:07:54 +02:00
|
|
|
'videoQuota': USER_VIDEO_QUOTA.MESSAGES
|
2017-06-16 14:32:15 +02:00
|
|
|
}
|
2016-08-09 21:45:21 +02:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
constructor (
|
2017-10-19 17:33:32 +02:00
|
|
|
protected serverService: ServerService,
|
2016-09-09 22:16:51 +02:00
|
|
|
private formBuilder: FormBuilder,
|
|
|
|
private router: Router,
|
2017-01-27 16:14:11 +01:00
|
|
|
private notificationsService: NotificationsService,
|
2018-06-04 16:21:17 +02:00
|
|
|
private userService: UserService,
|
|
|
|
private i18n: I18n
|
2016-09-09 22:16:51 +02:00
|
|
|
) {
|
2017-06-16 14:32:15 +02:00
|
|
|
super()
|
2016-09-09 22:16:51 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
buildForm () {
|
2016-09-09 22:16:51 +02:00
|
|
|
this.form = this.formBuilder.group({
|
|
|
|
username: [ '', USER_USERNAME.VALIDATORS ],
|
2017-02-18 09:29:59 +01:00
|
|
|
email: [ '', USER_EMAIL.VALIDATORS ],
|
2017-09-04 20:07:54 +02:00
|
|
|
password: [ '', USER_PASSWORD.VALIDATORS ],
|
2017-10-27 16:55:03 +02:00
|
|
|
role: [ UserRole.USER, USER_ROLE.VALIDATORS ],
|
2017-09-04 20:07:54 +02:00
|
|
|
videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
|
2017-06-16 14:32:15 +02:00
|
|
|
})
|
2016-09-09 22:16:51 +02:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
|
2016-09-09 22:16:51 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
ngOnInit () {
|
|
|
|
this.buildForm()
|
2016-08-09 21:45:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-05 21:29:39 +02:00
|
|
|
formValidated () {
|
|
|
|
this.error = undefined
|
2016-08-09 21:45:21 +02:00
|
|
|
|
2017-07-10 19:43:21 +02:00
|
|
|
const userCreate: UserCreate = this.form.value
|
2016-09-09 22:16:51 +02:00
|
|
|
|
2017-09-04 20:07:54 +02:00
|
|
|
// A select in HTML is always mapped as a string, we convert it to number
|
|
|
|
userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
|
|
|
|
|
2017-07-10 19:43:21 +02:00
|
|
|
this.userService.addUser(userCreate).subscribe(
|
2017-01-27 16:14:11 +01:00
|
|
|
() => {
|
2018-06-04 16:21:17 +02:00
|
|
|
this.notificationsService.success(
|
|
|
|
this.i18n('Success'),
|
|
|
|
this.i18n('User {{ username }} created.', { username: userCreate.username })
|
|
|
|
)
|
2017-06-16 14:32:15 +02:00
|
|
|
this.router.navigate([ '/admin/users/list' ])
|
2017-01-27 16:14:11 +01:00
|
|
|
},
|
2016-08-09 21:45:21 +02:00
|
|
|
|
2017-11-04 18:20:13 +01:00
|
|
|
err => this.error = err.message
|
2017-06-16 14:32:15 +02:00
|
|
|
)
|
2016-08-09 21:45:21 +02:00
|
|
|
}
|
2017-09-05 21:29:39 +02:00
|
|
|
|
|
|
|
isCreation () {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
getFormButtonTitle () {
|
2018-06-04 16:21:17 +02:00
|
|
|
return this.i18n('Create user')
|
2017-09-05 21:29:39 +02:00
|
|
|
}
|
2016-08-09 21:45:21 +02:00
|
|
|
}
|