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 { ServerService } from '../../../core'
6 import { UserEdit } from './user-edit'
7 import { User, UserUpdate } from '../../../../../../shared'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
11 import { ConfigService } from '@app/+admin/config/shared/config.service'
12 import { UserService } from '@app/shared'
15 selector: 'my-user-update',
16 templateUrl: './user-edit.component.html',
17 styleUrls: [ './user-edit.component.scss' ]
19 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
24 private paramsSub: Subscription
27 protected formValidatorService: FormValidatorService,
28 protected serverService: ServerService,
29 protected configService: ConfigService,
30 private userValidatorsService: UserValidatorsService,
31 private route: ActivatedRoute,
32 private router: Router,
33 private notificationsService: NotificationsService,
34 private userService: UserService,
39 this.buildQuotaOptions()
43 const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
45 email: this.userValidatorsService.USER_EMAIL,
46 role: this.userValidatorsService.USER_ROLE,
47 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
48 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
51 this.paramsSub = this.route.params.subscribe(routeParams => {
52 const userId = routeParams['id']
53 this.userService.getUser(userId).subscribe(
54 user => this.onUserFetched(user),
56 err => this.error = err.message
62 this.paramsSub.unsubscribe()
66 this.error = undefined
68 const userUpdate: UserUpdate = this.form.value
70 // A select in HTML is always mapped as a string, we convert it to number
71 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
72 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
74 this.userService.updateUser(this.userId, userUpdate).subscribe(
76 this.notificationsService.success(
78 this.i18n('User {{username}} updated.', { username: this.username })
80 this.router.navigate([ '/admin/users/list' ])
83 err => this.error = err.message
91 getFormButtonTitle () {
92 return this.i18n('Update user')
95 private onUserFetched (userJson: User) {
96 this.userId = userJson.id
97 this.username = userJson.username
99 this.form.patchValue({
100 email: userJson.email,
102 videoQuota: userJson.videoQuota,
103 videoQuotaDaily: userJson.videoQuotaDaily