1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { Subscription } from 'rxjs/Subscription'
6 import { NotificationsService } from 'angular2-notifications'
8 import { UserService } from '../shared'
14 } from '../../../shared'
15 import { ServerService } from '../../../core'
16 import { UserEdit } from './user-edit'
17 import { UserUpdate, UserRole } from '../../../../../../shared'
20 selector: 'my-user-update',
21 templateUrl: './user-edit.component.html',
22 styleUrls: [ './user-edit.component.scss' ]
24 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
35 validationMessages = {
36 'email': USER_EMAIL.MESSAGES,
37 'role': USER_ROLE.MESSAGES,
38 'videoQuota': USER_VIDEO_QUOTA.MESSAGES
41 private paramsSub: Subscription
44 protected serverService: ServerService,
45 private route: ActivatedRoute,
46 private router: Router,
47 private notificationsService: NotificationsService,
48 private formBuilder: FormBuilder,
49 private userService: UserService
55 this.form = this.formBuilder.group({
56 email: [ '', USER_EMAIL.VALIDATORS ],
57 role: [ '', USER_ROLE.VALIDATORS ],
58 videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
61 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
67 this.paramsSub = this.route.params.subscribe(routeParams => {
68 const userId = routeParams['id']
69 this.userService.getUser(userId).subscribe(
70 user => this.onUserFetched(user),
72 err => this.error = err.message
78 this.paramsSub.unsubscribe()
82 this.error = undefined
84 const userUpdate: UserUpdate = this.form.value
86 // A select in HTML is always mapped as a string, we convert it to number
87 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
89 this.userService.updateUser(this.userId, userUpdate).subscribe(
91 this.notificationsService.success('Success', `User ${this.username} updated.`)
92 this.router.navigate([ '/admin/users/list' ])
95 err => this.error = err.message
103 getFormButtonTitle () {
107 private onUserFetched (userJson: User) {
108 this.userId = userJson.id
109 this.username = userJson.username
111 this.form.patchValue({
112 email: userJson.email,
114 videoQuota: userJson.videoQuota