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 { User } from '../../../shared'
7 import { ServerService } from '../../../core'
8 import { UserEdit } from './user-edit'
9 import { UserUpdate } from '../../../../../../shared'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
12 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
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 private userValidatorsService: UserValidatorsService,
30 private route: ActivatedRoute,
31 private router: Router,
32 private notificationsService: NotificationsService,
33 private userService: UserService,
40 const defaultValues = { videoQuota: '-1' }
42 email: this.userValidatorsService.USER_EMAIL,
43 role: this.userValidatorsService.USER_ROLE,
44 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA
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)
69 this.userService.updateUser(this.userId, userUpdate).subscribe(
71 this.notificationsService.success(
73 this.i18n('User {{username}} updated.', { username: this.username })
75 this.router.navigate([ '/admin/users/list' ])
78 err => this.error = err.message
86 getFormButtonTitle () {
87 return this.i18n('Update user')
90 private onUserFetched (userJson: User) {
91 this.userId = userJson.id
92 this.username = userJson.username
94 this.form.patchValue({
95 email: userJson.email,
97 videoQuota: userJson.videoQuota