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'
9 import { USER_EMAIL, USER_VIDEO_QUOTA } from '../../../shared'
10 import { UserUpdate } from '../../../../../../shared/models/users/user-update.model'
11 import { User } from '../../../shared/users/user.model'
12 import { UserEdit } from './user-edit'
15 selector: 'my-user-update',
16 templateUrl: './user-edit.component.html'
18 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
28 validationMessages = {
29 'email': USER_EMAIL.MESSAGES,
30 'videoQuota': USER_VIDEO_QUOTA.MESSAGES
33 private paramsSub: Subscription
36 private formBuilder: FormBuilder,
37 private route: ActivatedRoute,
38 private router: Router,
39 private notificationsService: NotificationsService,
40 private userService: UserService
46 this.form = this.formBuilder.group({
47 email: [ '', USER_EMAIL.VALIDATORS ],
48 videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
51 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
57 this.paramsSub = this.route.params.subscribe(routeParams => {
58 const userId = routeParams['id']
59 this.userService.getUser(userId).subscribe(
60 user => this.onUserFetched(user),
62 err => this.error = err.text
68 this.paramsSub.unsubscribe()
72 this.error = undefined
74 const userUpdate: UserUpdate = this.form.value
76 // A select in HTML is always mapped as a string, we convert it to number
77 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
79 this.userService.updateUser(this.userId, userUpdate).subscribe(
81 this.notificationsService.success('Success', `User ${this.username} updated.`)
82 this.router.navigate([ '/admin/users/list' ])
85 err => this.error = err.text
93 getFormButtonTitle () {
97 private onUserFetched (userJson: User) {
98 this.userId = userJson.id
99 this.username = userJson.username
101 this.form.patchValue({
102 email: userJson.email,
103 videoQuota: userJson.videoQuota