1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Subscription } from 'rxjs'
4 import { NotificationsService } from 'angular2-notifications'
5 import { UserService } from '../shared'
6 import { ServerService } from '../../../core'
7 import { UserEdit } from './user-edit'
8 import { UserUpdate, User } from '../../../../../../shared'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
11 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
14 selector: 'my-user-update',
15 templateUrl: './user-edit.component.html',
16 styleUrls: [ './user-edit.component.scss' ]
18 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
23 private paramsSub: Subscription
26 protected formValidatorService: FormValidatorService,
27 protected serverService: ServerService,
28 private userValidatorsService: UserValidatorsService,
29 private route: ActivatedRoute,
30 private router: Router,
31 private notificationsService: NotificationsService,
32 private userService: UserService,
39 const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
41 email: this.userValidatorsService.USER_EMAIL,
42 role: this.userValidatorsService.USER_ROLE,
43 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
44 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
47 this.paramsSub = this.route.params.subscribe(routeParams => {
48 const userId = routeParams['id']
49 this.userService.getUser(userId).subscribe(
50 user => this.onUserFetched(user),
52 err => this.error = err.message
58 this.paramsSub.unsubscribe()
62 this.error = undefined
64 const userUpdate: UserUpdate = this.form.value
66 // A select in HTML is always mapped as a string, we convert it to number
67 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
68 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
70 this.userService.updateUser(this.userId, userUpdate).subscribe(
72 this.notificationsService.success(
74 this.i18n('User {{username}} updated.', { username: this.username })
76 this.router.navigate([ '/admin/users/list' ])
79 err => this.error = err.message
87 getFormButtonTitle () {
88 return this.i18n('Update user')
91 private onUserFetched (userJson: User) {
92 this.userId = userJson.id
93 this.username = userJson.username
95 this.form.patchValue({
96 email: userJson.email,
98 videoQuota: userJson.videoQuota,
99 videoQuotaDaily: userJson.videoQuotaDaily