2017-06-16 14:32:15 +02:00
|
|
|
import { Component, OnInit } from '@angular/core'
|
|
|
|
import { Router } from '@angular/router'
|
|
|
|
import { NotificationsService } from 'angular2-notifications'
|
|
|
|
import { UserService } 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'
|
2018-06-05 10:58:45 +02:00
|
|
|
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
|
2018-06-05 15:01:45 +02:00
|
|
|
import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
|
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
|
|
|
constructor (
|
2017-10-19 17:33:32 +02:00
|
|
|
protected serverService: ServerService,
|
2018-06-05 10:58:45 +02:00
|
|
|
protected formValidatorService: FormValidatorService,
|
2018-06-05 15:01:45 +02:00
|
|
|
private userValidatorsService: UserValidatorsService,
|
2016-09-09 22:16:51 +02:00
|
|
|
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
|
|
|
ngOnInit () {
|
2018-06-05 10:58:45 +02:00
|
|
|
const defaultValues = {
|
|
|
|
role: UserRole.USER.toString(),
|
|
|
|
videoQuota: '-1'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buildForm({
|
2018-06-05 15:01:45 +02:00
|
|
|
username: this.userValidatorsService.USER_USERNAME,
|
|
|
|
email: this.userValidatorsService.USER_EMAIL,
|
|
|
|
password: this.userValidatorsService.USER_PASSWORD,
|
|
|
|
role: this.userValidatorsService.USER_ROLE,
|
|
|
|
videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA
|
2018-06-05 10:58:45 +02:00
|
|
|
}, defaultValues)
|
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'),
|
2018-06-05 09:13:26 +02:00
|
|
|
this.i18n('User {{username}} created.', { username: userCreate.username })
|
2018-06-04 16:21:17 +02:00
|
|
|
)
|
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
|
|
|
}
|