]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-edit/user-update.component.ts
Move user moderation tool in a separate component
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Subscription } from 'rxjs'
4 import { NotificationsService } from 'angular2-notifications'
5 import { ServerService } from '../../../core'
6 import { UserEdit } from './user-edit'
7 import { User, UserUpdate } from '../../../../../../shared'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
11 import { ConfigService } from '@app/+admin/config/shared/config.service'
12 import { UserService } from '@app/shared'
13
14 @Component({
15 selector: 'my-user-update',
16 templateUrl: './user-edit.component.html',
17 styleUrls: [ './user-edit.component.scss' ]
18 })
19 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
20 error: string
21 userId: number
22 username: string
23
24 private paramsSub: Subscription
25
26 constructor (
27 protected formValidatorService: FormValidatorService,
28 protected serverService: ServerService,
29 protected configService: ConfigService,
30 private userValidatorsService: UserValidatorsService,
31 private route: ActivatedRoute,
32 private router: Router,
33 private notificationsService: NotificationsService,
34 private userService: UserService,
35 private i18n: I18n
36 ) {
37 super()
38
39 this.buildQuotaOptions()
40 }
41
42 ngOnInit () {
43 const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
44 this.buildForm({
45 email: this.userValidatorsService.USER_EMAIL,
46 role: this.userValidatorsService.USER_ROLE,
47 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
48 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
49 }, defaultValues)
50
51 this.paramsSub = this.route.params.subscribe(routeParams => {
52 const userId = routeParams['id']
53 this.userService.getUser(userId).subscribe(
54 user => this.onUserFetched(user),
55
56 err => this.error = err.message
57 )
58 })
59 }
60
61 ngOnDestroy () {
62 this.paramsSub.unsubscribe()
63 }
64
65 formValidated () {
66 this.error = undefined
67
68 const userUpdate: UserUpdate = this.form.value
69
70 // A select in HTML is always mapped as a string, we convert it to number
71 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
72 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
73
74 this.userService.updateUser(this.userId, userUpdate).subscribe(
75 () => {
76 this.notificationsService.success(
77 this.i18n('Success'),
78 this.i18n('User {{username}} updated.', { username: this.username })
79 )
80 this.router.navigate([ '/admin/users/list' ])
81 },
82
83 err => this.error = err.message
84 )
85 }
86
87 isCreation () {
88 return false
89 }
90
91 getFormButtonTitle () {
92 return this.i18n('Update user')
93 }
94
95 private onUserFetched (userJson: User) {
96 this.userId = userJson.id
97 this.username = userJson.username
98
99 this.form.patchValue({
100 email: userJson.email,
101 role: userJson.role,
102 videoQuota: userJson.videoQuota,
103 videoQuotaDaily: userJson.videoQuotaDaily
104 })
105 }
106 }