]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-edit/user-update.component.ts
Migrate to $localize
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
1 import { Subscription } from 'rxjs'
2 import { Component, OnDestroy, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { ConfigService } from '@app/+admin/config/shared/config.service'
5 import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core'
6 import { FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms'
7 import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models'
8 import { UserEdit } from './user-edit'
9
10 @Component({
11 selector: 'my-user-update',
12 templateUrl: './user-edit.component.html',
13 styleUrls: [ './user-edit.component.scss' ]
14 })
15 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
16 error: string
17
18 private paramsSub: Subscription
19
20 constructor (
21 protected formValidatorService: FormValidatorService,
22 protected serverService: ServerService,
23 protected configService: ConfigService,
24 protected screenService: ScreenService,
25 protected auth: AuthService,
26 private userValidatorsService: UserValidatorsService,
27 private route: ActivatedRoute,
28 private router: Router,
29 private notifier: Notifier,
30 private userService: UserService
31 ) {
32 super()
33
34 this.buildQuotaOptions()
35 }
36
37 ngOnInit () {
38 super.ngOnInit()
39
40 const defaultValues = {
41 role: UserRole.USER.toString(),
42 videoQuota: '-1',
43 videoQuotaDaily: '-1'
44 }
45
46 this.buildForm({
47 email: this.userValidatorsService.USER_EMAIL,
48 role: this.userValidatorsService.USER_ROLE,
49 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
50 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
51 byPassAutoBlock: null
52 }, defaultValues)
53
54 this.paramsSub = this.route.params.subscribe(routeParams => {
55 const userId = routeParams['id']
56 this.userService.getUser(userId, true).subscribe(
57 user => this.onUserFetched(user),
58
59 err => this.error = err.message
60 )
61 })
62 }
63
64 ngOnDestroy () {
65 this.paramsSub.unsubscribe()
66 }
67
68 formValidated () {
69 this.error = undefined
70
71 const userUpdate: UserUpdate = this.form.value
72 userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
73
74 // A select in HTML is always mapped as a string, we convert it to number
75 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
76 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
77
78 this.userService.updateUser(this.user.id, userUpdate).subscribe(
79 () => {
80 this.notifier.success($localize`User ${this.user.username} updated.`)
81 this.router.navigate([ '/admin/users/list' ])
82 },
83
84 err => this.error = err.message
85 )
86 }
87
88 isCreation () {
89 return false
90 }
91
92 isPasswordOptional () {
93 return false
94 }
95
96 getFormButtonTitle () {
97 return $localize`Update user`
98 }
99
100 resetPassword () {
101 this.userService.askResetPassword(this.user.email).subscribe(
102 () => {
103 this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`)
104 },
105
106 err => this.error = err.message
107 )
108 }
109
110 private onUserFetched (userJson: UserType) {
111 this.user = new User(userJson)
112
113 this.form.patchValue({
114 email: userJson.email,
115 role: userJson.role.toString(),
116 videoQuota: userJson.videoQuota,
117 videoQuotaDaily: userJson.videoQuotaDaily,
118 byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
119 })
120 }
121 }