]>
Commit | Line | Data |
---|---|---|
b426edd4 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
8094a898 | 2 | import { ActivatedRoute, Router } from '@angular/router' |
db400f44 | 3 | import { Subscription } from 'rxjs' |
f8b2c1b4 | 4 | import { Notifier } from '@app/core' |
6a84aafd | 5 | import { ServerService } from '../../../core' |
8094a898 | 6 | import { UserEdit } from './user-edit' |
3827c3b3 | 7 | import { User, UserUpdate } from '../../../../../../shared' |
b1d40cff | 8 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 9 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 10 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' |
3827c3b3 | 11 | import { ConfigService } from '@app/+admin/config/shared/config.service' |
e724fa93 | 12 | import { 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 | }) |
19 | export 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 | () => { | |
b426edd4 | 96 | this.notifier.success( |
328c78bc RK |
97 | this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username }) |
98 | ) | |
99 | }, | |
100 | ||
101 | err => this.error = err.message | |
102 | ) | |
103 | } | |
104 | ||
8094a898 C |
105 | private onUserFetched (userJson: User) { |
106 | this.userId = userJson.id | |
107 | this.username = userJson.username | |
328c78bc | 108 | this.userEmail = userJson.email |
8094a898 C |
109 | |
110 | this.form.patchValue({ | |
111 | email: userJson.email, | |
954605a8 | 112 | role: userJson.role, |
bee0abff FA |
113 | videoQuota: userJson.videoQuota, |
114 | videoQuotaDaily: userJson.videoQuotaDaily | |
8094a898 C |
115 | }) |
116 | } | |
117 | } |