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