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'
5 import { NotificationsService } from 'angular2-notifications'
6 import { UserService } from '../shared'
7 import { User, USER_EMAIL, USER_ROLE, USER_VIDEO_QUOTA } from '../../../shared'
8 import { ServerService } from '../../../core'
9 import { UserEdit } from './user-edit'
10 import { UserUpdate } from '../../../../../../shared'
13 selector: 'my-user-update',
14 templateUrl: './user-edit.component.html',
15 styleUrls: [ './user-edit.component.scss' ]
17 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
28 validationMessages = {
29 'email': USER_EMAIL.MESSAGES,
30 'role': USER_ROLE.MESSAGES,
31 'videoQuota': USER_VIDEO_QUOTA.MESSAGES
34 private paramsSub: Subscription
37 protected serverService: ServerService,
38 private route: ActivatedRoute,
39 private router: Router,
40 private notificationsService: NotificationsService,
41 private formBuilder: FormBuilder,
42 private userService: UserService
48 this.form = this.formBuilder.group({
49 email: [ '', USER_EMAIL.VALIDATORS ],
50 role: [ '', USER_ROLE.VALIDATORS ],
51 videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
54 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
60 this.paramsSub = this.route.params.subscribe(routeParams => {
61 const userId = routeParams['id']
62 this.userService.getUser(userId).subscribe(
63 user => this.onUserFetched(user),
65 err => this.error = err.message
71 this.paramsSub.unsubscribe()
75 this.error = undefined
77 const userUpdate: UserUpdate = this.form.value
79 // A select in HTML is always mapped as a string, we convert it to number
80 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
82 this.userService.updateUser(this.userId, userUpdate).subscribe(
84 this.notificationsService.success('Success', `User ${this.username} updated.`)
85 this.router.navigate([ '/admin/users/list' ])
88 err => this.error = err.message
96 getFormButtonTitle () {
100 private onUserFetched (userJson: User) {
101 this.userId = userJson.id
102 this.username = userJson.username
104 this.form.patchValue({
105 email: userJson.email,
107 videoQuota: userJson.videoQuota