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

116 lines
3.0 KiB
TypeScript
Raw Normal View History

2017-09-05 21:29:39 +02:00
import { Component, OnDestroy, OnInit } from '@angular/core'
import { FormBuilder, FormGroup } from '@angular/forms'
import { ActivatedRoute, Router } from '@angular/router'
2018-05-15 11:55:51 +02:00
import { Subscription } from 'rxjs'
2017-09-05 21:29:39 +02:00
import { NotificationsService } from 'angular2-notifications'
import { UserService } from '../shared'
2018-05-15 11:55:51 +02:00
import { User, USER_EMAIL, USER_ROLE, USER_VIDEO_QUOTA } from '../../../shared'
import { ServerService } from '../../../core'
2017-09-05 21:29:39 +02:00
import { UserEdit } from './user-edit'
2018-05-15 11:55:51 +02:00
import { UserUpdate } from '../../../../../../shared'
2018-06-04 16:21:17 +02:00
import { I18n } from '@ngx-translate/i18n-polyfill'
2017-09-05 21:29:39 +02:00
@Component({
selector: 'my-user-update',
templateUrl: './user-edit.component.html',
styleUrls: [ './user-edit.component.scss' ]
2017-09-05 21:29:39 +02:00
})
export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
error: string
userId: number
username: string
form: FormGroup
formErrors = {
'email': '',
'role': '',
2017-09-05 21:29:39 +02:00
'videoQuota': ''
}
validationMessages = {
'email': USER_EMAIL.MESSAGES,
'role': USER_ROLE.MESSAGES,
2017-09-05 21:29:39 +02:00
'videoQuota': USER_VIDEO_QUOTA.MESSAGES
}
private paramsSub: Subscription
constructor (
protected serverService: ServerService,
2017-09-05 21:29:39 +02:00
private route: ActivatedRoute,
private router: Router,
private notificationsService: NotificationsService,
private formBuilder: FormBuilder,
2018-06-04 16:21:17 +02:00
private userService: UserService,
private i18n: I18n
2017-09-05 21:29:39 +02:00
) {
super()
}
buildForm () {
this.form = this.formBuilder.group({
email: [ '', USER_EMAIL.VALIDATORS ],
role: [ '', USER_ROLE.VALIDATORS ],
2017-09-05 21:29:39 +02:00
videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
})
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
}
ngOnInit () {
this.buildForm()
this.paramsSub = this.route.params.subscribe(routeParams => {
const userId = routeParams['id']
this.userService.getUser(userId).subscribe(
user => this.onUserFetched(user),
2017-11-04 18:20:13 +01:00
err => this.error = err.message
2017-09-05 21:29:39 +02:00
)
})
}
ngOnDestroy () {
this.paramsSub.unsubscribe()
}
formValidated () {
this.error = undefined
const userUpdate: UserUpdate = this.form.value
// A select in HTML is always mapped as a string, we convert it to number
userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
this.userService.updateUser(this.userId, userUpdate).subscribe(
() => {
2018-06-04 16:21:17 +02:00
this.notificationsService.success(
this.i18n('Success'),
this.i18n('User {{ username }} updated.', { username: this.username })
)
2017-09-05 21:29:39 +02:00
this.router.navigate([ '/admin/users/list' ])
},
2017-11-04 18:20:13 +01:00
err => this.error = err.message
2017-09-05 21:29:39 +02:00
)
}
isCreation () {
return false
}
getFormButtonTitle () {
2018-06-04 16:21:17 +02:00
return this.i18n('Update user')
2017-09-05 21:29:39 +02:00
}
private onUserFetched (userJson: User) {
this.userId = userJson.id
this.username = userJson.username
this.form.patchValue({
email: userJson.email,
role: userJson.role,
2017-09-05 21:29:39 +02:00
videoQuota: userJson.videoQuota
})
}
}