PeerTube/client/src/app/+admin/users/user-edit/user-create.component.ts

92 lines
2.5 KiB
TypeScript
Raw Normal View History

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'
import { ServerService } from '../../../core'
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',
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
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': '',
'role': '',
2017-09-04 20:07:54 +02:00
'videoQuota': ''
}
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,
'role': USER_ROLE.MESSAGES,
2017-09-04 20:07:54 +02:00
'videoQuota': USER_VIDEO_QUOTA.MESSAGES
}
2016-08-09 21:45:21 +02:00
constructor (
protected serverService: ServerService,
2016-09-09 22:16:51 +02:00
private formBuilder: FormBuilder,
private router: Router,
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
) {
super()
2016-09-09 22:16:51 +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 ],
role: [ UserRole.USER, USER_ROLE.VALIDATORS ],
2017-09-04 20:07:54 +02:00
videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
})
2016-09-09 22:16:51 +02:00
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
2016-09-09 22:16:51 +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
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)
this.userService.addUser(userCreate).subscribe(
() => {
2018-06-04 16:21:17 +02:00
this.notificationsService.success(
this.i18n('Success'),
this.i18n('User {{ username }} created.', { username: userCreate.username })
)
this.router.navigate([ '/admin/users/list' ])
},
2016-08-09 21:45:21 +02:00
2017-11-04 18:20:13 +01:00
err => this.error = err.message
)
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
}