]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
8094a898 1import { Component, OnDestroy, OnInit } from '@angular/core'
8094a898 2import { ActivatedRoute, Router } from '@angular/router'
db400f44 3import { Subscription } from 'rxjs'
8094a898 4import { NotificationsService } from 'angular2-notifications'
6a84aafd 5import { ServerService } from '../../../core'
8094a898 6import { UserEdit } from './user-edit'
3827c3b3 7import { User, UserUpdate } from '../../../../../../shared'
b1d40cff 8import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 9import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e309822b 10import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
3827c3b3 11import { ConfigService } from '@app/+admin/config/shared/config.service'
e724fa93 12import { UserService } from '@app/shared'
8094a898
C
13
14@Component({
15 selector: 'my-user-update',
6a84aafd
C
16 templateUrl: './user-edit.component.html',
17 styleUrls: [ './user-edit.component.scss' ]
8094a898
C
18})
19export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
20 error: string
21 userId: number
22 username: string
23
8094a898
C
24 private paramsSub: Subscription
25
26 constructor (
d18d6478 27 protected formValidatorService: FormValidatorService,
6a84aafd 28 protected serverService: ServerService,
3827c3b3 29 protected configService: ConfigService,
e309822b 30 private userValidatorsService: UserValidatorsService,
8094a898
C
31 private route: ActivatedRoute,
32 private router: Router,
33 private notificationsService: NotificationsService,
b1d40cff
C
34 private userService: UserService,
35 private i18n: I18n
8094a898
C
36 ) {
37 super()
3827c3b3
C
38
39 this.buildQuotaOptions()
8094a898
C
40 }
41
8094a898 42 ngOnInit () {
bee0abff 43 const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
d18d6478 44 this.buildForm({
e309822b
C
45 email: this.userValidatorsService.USER_EMAIL,
46 role: this.userValidatorsService.USER_ROLE,
bee0abff
FA
47 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
48 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
d18d6478 49 }, defaultValues)
8094a898
C
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
f7354483 56 err => this.error = err.message
8094a898
C
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)
bee0abff 72 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
8094a898
C
73
74 this.userService.updateUser(this.userId, userUpdate).subscribe(
75 () => {
b1d40cff
C
76 this.notificationsService.success(
77 this.i18n('Success'),
25acef90 78 this.i18n('User {{username}} updated.', { username: this.username })
b1d40cff 79 )
8094a898
C
80 this.router.navigate([ '/admin/users/list' ])
81 },
82
f7354483 83 err => this.error = err.message
8094a898
C
84 )
85 }
86
87 isCreation () {
88 return false
89 }
90
91 getFormButtonTitle () {
b1d40cff 92 return this.i18n('Update user')
8094a898
C
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,
954605a8 101 role: userJson.role,
bee0abff
FA
102 videoQuota: userJson.videoQuota,
103 videoQuotaDaily: userJson.videoQuotaDaily
8094a898
C
104 })
105 }
106}