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

78 lines
2.4 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from '@angular/core'
import { Router } from '@angular/router'
import { NotificationsService } from 'angular2-notifications'
import { UserService } 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'
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',
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
constructor (
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,
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
}
ngOnInit () {
2018-06-05 10:58:45 +02:00
const defaultValues = {
role: UserRole.USER.toString(),
2018-08-31 10:05:54 +02:00
videoQuota: '-1',
videoQuotaDaily: '-1'
2018-06-05 10:58:45 +02:00
}
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,
2018-08-31 10:05:54 +02:00
videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
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
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'),
2018-06-05 09:13:26 +02:00
this.i18n('User {{username}} created.', { username: userCreate.username })
2018-06-04 16:21:17 +02:00
)
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
}