]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-edit/user-update.component.ts
cleanup and remove paramSubs
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
CommitLineData
328c78bc 1import { Component, OnDestroy, OnInit, Input } from '@angular/core'
8094a898 2import { ActivatedRoute, Router } from '@angular/router'
db400f44 3import { Subscription } from 'rxjs'
f8b2c1b4 4import { Notifier } from '@app/core'
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
328c78bc 22 userEmail: string
8094a898
C
23 username: string
24
8094a898
C
25 private paramsSub: Subscription
26
27 constructor (
d18d6478 28 protected formValidatorService: FormValidatorService,
6a84aafd 29 protected serverService: ServerService,
3827c3b3 30 protected configService: ConfigService,
e309822b 31 private userValidatorsService: UserValidatorsService,
8094a898
C
32 private route: ActivatedRoute,
33 private router: Router,
f8b2c1b4 34 private notifier: Notifier,
b1d40cff
C
35 private userService: UserService,
36 private i18n: I18n
8094a898
C
37 ) {
38 super()
3827c3b3
C
39
40 this.buildQuotaOptions()
8094a898
C
41 }
42
8094a898 43 ngOnInit () {
bee0abff 44 const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
d18d6478 45 this.buildForm({
e309822b
C
46 email: this.userValidatorsService.USER_EMAIL,
47 role: this.userValidatorsService.USER_ROLE,
bee0abff
FA
48 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
49 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
d18d6478 50 }, defaultValues)
8094a898
C
51
52 this.paramsSub = this.route.params.subscribe(routeParams => {
53 const userId = routeParams['id']
54 this.userService.getUser(userId).subscribe(
55 user => this.onUserFetched(user),
56
f7354483 57 err => this.error = err.message
8094a898
C
58 )
59 })
60 }
61
62 ngOnDestroy () {
63 this.paramsSub.unsubscribe()
64 }
65
66 formValidated () {
67 this.error = undefined
68
69 const userUpdate: UserUpdate = this.form.value
70
71 // A select in HTML is always mapped as a string, we convert it to number
72 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
bee0abff 73 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
8094a898
C
74
75 this.userService.updateUser(this.userId, userUpdate).subscribe(
76 () => {
f8b2c1b4 77 this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
8094a898
C
78 this.router.navigate([ '/admin/users/list' ])
79 },
80
f7354483 81 err => this.error = err.message
8094a898
C
82 )
83 }
84
85 isCreation () {
86 return false
87 }
88
89 getFormButtonTitle () {
b1d40cff 90 return this.i18n('Update user')
8094a898
C
91 }
92
328c78bc
RK
93 resetPassword () {
94 this.userService.askResetPassword(this.userEmail).subscribe(
95 () => {
96 this.notificationsService.success(
97 this.i18n('Success'),
98 this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username })
99 )
100 },
101
102 err => this.error = err.message
103 )
104 }
105
8094a898
C
106 private onUserFetched (userJson: User) {
107 this.userId = userJson.id
108 this.username = userJson.username
328c78bc 109 this.userEmail = userJson.email
8094a898
C
110
111 this.form.patchValue({
112 email: userJson.email,
954605a8 113 role: userJson.role,
bee0abff
FA
114 videoQuota: userJson.videoQuota,
115 videoQuotaDaily: userJson.videoQuotaDaily
8094a898
C
116 })
117 }
118}