Take in account transcoding for video quota
[oweals/peertube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
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'
5
6 import { NotificationsService } from 'angular2-notifications'
7
8 import { UserService } from '../shared'
9 import { USER_EMAIL, USER_VIDEO_QUOTA } from '../../../shared'
10 import { ServerService } from '../../../core'
11 import { UserUpdate } from '../../../../../../shared/models/users/user-update.model'
12 import { User } from '../../../shared/users/user.model'
13 import { UserEdit } from './user-edit'
14
15 @Component({
16   selector: 'my-user-update',
17   templateUrl: './user-edit.component.html',
18   styleUrls: [ './user-edit.component.scss' ]
19 })
20 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
21   error: string
22   userId: number
23   username: string
24
25   form: FormGroup
26   formErrors = {
27     'email': '',
28     'videoQuota': ''
29   }
30   validationMessages = {
31     'email': USER_EMAIL.MESSAGES,
32     'videoQuota': USER_VIDEO_QUOTA.MESSAGES
33   }
34
35   private paramsSub: Subscription
36
37   constructor (
38     protected serverService: ServerService,
39     private route: ActivatedRoute,
40     private router: Router,
41     private notificationsService: NotificationsService,
42     private formBuilder: FormBuilder,
43     private userService: UserService
44   ) {
45     super()
46   }
47
48   buildForm () {
49     this.form = this.formBuilder.group({
50       email:    [ '', USER_EMAIL.VALIDATORS ],
51       videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
52     })
53
54     this.form.valueChanges.subscribe(data => this.onValueChanged(data))
55   }
56
57   ngOnInit () {
58     this.buildForm()
59
60     this.paramsSub = this.route.params.subscribe(routeParams => {
61       const userId = routeParams['id']
62       this.userService.getUser(userId).subscribe(
63         user => this.onUserFetched(user),
64
65         err => this.error = err
66       )
67     })
68   }
69
70   ngOnDestroy () {
71     this.paramsSub.unsubscribe()
72   }
73
74   formValidated () {
75     this.error = undefined
76
77     const userUpdate: UserUpdate = this.form.value
78
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)
81
82     this.userService.updateUser(this.userId, userUpdate).subscribe(
83       () => {
84         this.notificationsService.success('Success', `User ${this.username} updated.`)
85         this.router.navigate([ '/admin/users/list' ])
86       },
87
88       err => this.error = err
89     )
90   }
91
92   isCreation () {
93     return false
94   }
95
96   getFormButtonTitle () {
97     return 'Update user'
98   }
99
100   private onUserFetched (userJson: User) {
101     this.userId = userJson.id
102     this.username = userJson.username
103
104     this.form.patchValue({
105       email: userJson.email,
106       videoQuota: userJson.videoQuota
107     })
108   }
109 }