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

103 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 { 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'
import { ServerService } from '../../../core'
2017-09-05 21:29:39 +02:00
import { UserEdit } from './user-edit'
2018-08-14 19:24:56 +02:00
import { UserUpdate, User } from '../../../../../../shared'
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'
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
private paramsSub: Subscription
constructor (
2018-06-05 10:58:45 +02:00
protected formValidatorService: FormValidatorService,
protected serverService: ServerService,
2018-06-05 15:01:45 +02:00
private userValidatorsService: UserValidatorsService,
2017-09-05 21:29:39 +02:00
private route: ActivatedRoute,
private router: Router,
private notificationsService: NotificationsService,
2018-06-04 16:21:17 +02:00
private userService: UserService,
private i18n: I18n
2017-09-05 21:29:39 +02:00
) {
super()
}
ngOnInit () {
const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
2018-06-05 10:58:45 +02:00
this.buildForm({
2018-06-05 15:01:45 +02:00
email: this.userValidatorsService.USER_EMAIL,
role: this.userValidatorsService.USER_ROLE,
videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
2018-06-05 10:58:45 +02:00
}, defaultValues)
2017-09-05 21:29:39 +02:00
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)
userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
2017-09-05 21:29:39 +02:00
this.userService.updateUser(this.userId, userUpdate).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}} updated.', { username: this.username })
2018-06-04 16:21:17 +02:00
)
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,
videoQuota: userJson.videoQuota,
videoQuotaDaily: userJson.videoQuotaDaily
2017-09-05 21:29:39 +02:00
})
}
}