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