]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-edit/user-update.component.ts
Fix tag display on video watch
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
CommitLineData
8094a898
C
1import { Component, OnDestroy, OnInit } from '@angular/core'
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { ActivatedRoute, Router } from '@angular/router'
4import { Subscription } from 'rxjs/Subscription'
5
6import { NotificationsService } from 'angular2-notifications'
7
8import { UserService } from '../shared'
9import { USER_EMAIL, USER_VIDEO_QUOTA } from '../../../shared'
6a84aafd 10import { ServerService } from '../../../core'
8094a898
C
11import { UserUpdate } from '../../../../../../shared/models/users/user-update.model'
12import { User } from '../../../shared/users/user.model'
13import { UserEdit } from './user-edit'
14
15@Component({
16 selector: 'my-user-update',
6a84aafd
C
17 templateUrl: './user-edit.component.html',
18 styleUrls: [ './user-edit.component.scss' ]
8094a898
C
19})
20export 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 (
6a84aafd 38 protected serverService: ServerService,
8094a898
C
39 private route: ActivatedRoute,
40 private router: Router,
41 private notificationsService: NotificationsService,
6a84aafd 42 private formBuilder: FormBuilder,
8094a898
C
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
03b40f24 65 err => this.error = err
8094a898
C
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
03b40f24 88 err => this.error = err
8094a898
C
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}