]>
Commit | Line | Data |
---|---|---|
328c78bc | 1 | import { Component, OnDestroy, OnInit, Input } 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 | 23 | username: string |
328c78bc | 24 | isAdministration = false |
8094a898 | 25 | |
8094a898 | 26 | private paramsSub: Subscription |
328c78bc | 27 | private isAdministrationSub: Subscription |
8094a898 C |
28 | |
29 | constructor ( | |
d18d6478 | 30 | protected formValidatorService: FormValidatorService, |
6a84aafd | 31 | protected serverService: ServerService, |
3827c3b3 | 32 | protected configService: ConfigService, |
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 () { |
bee0abff | 46 | const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' } |
d18d6478 | 47 | this.buildForm({ |
e309822b C |
48 | email: this.userValidatorsService.USER_EMAIL, |
49 | role: this.userValidatorsService.USER_ROLE, | |
bee0abff FA |
50 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, |
51 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY | |
d18d6478 | 52 | }, defaultValues) |
8094a898 C |
53 | |
54 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
55 | const userId = routeParams['id'] | |
56 | this.userService.getUser(userId).subscribe( | |
57 | user => this.onUserFetched(user), | |
58 | ||
f7354483 | 59 | err => this.error = err.message |
8094a898 C |
60 | ) |
61 | }) | |
328c78bc RK |
62 | |
63 | this.isAdministrationSub = this.route.data.subscribe(data => { | |
64 | if (data.isAdministration) this.isAdministration = data.isAdministration | |
65 | }) | |
8094a898 C |
66 | } |
67 | ||
68 | ngOnDestroy () { | |
69 | this.paramsSub.unsubscribe() | |
328c78bc | 70 | this.isAdministrationSub.unsubscribe() |
8094a898 C |
71 | } |
72 | ||
73 | formValidated () { | |
74 | this.error = undefined | |
75 | ||
76 | const userUpdate: UserUpdate = this.form.value | |
77 | ||
78 | // A select in HTML is always mapped as a string, we convert it to number | |
79 | userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
bee0abff | 80 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
8094a898 C |
81 | |
82 | this.userService.updateUser(this.userId, userUpdate).subscribe( | |
83 | () => { | |
f8b2c1b4 | 84 | this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username })) |
8094a898 C |
85 | this.router.navigate([ '/admin/users/list' ]) |
86 | }, | |
87 | ||
f7354483 | 88 | err => this.error = err.message |
8094a898 C |
89 | ) |
90 | } | |
91 | ||
92 | isCreation () { | |
93 | return false | |
94 | } | |
95 | ||
96 | getFormButtonTitle () { | |
b1d40cff | 97 | return this.i18n('Update user') |
8094a898 C |
98 | } |
99 | ||
328c78bc RK |
100 | resetPassword () { |
101 | this.userService.askResetPassword(this.userEmail).subscribe( | |
102 | () => { | |
103 | this.notificationsService.success( | |
104 | this.i18n('Success'), | |
105 | this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username }) | |
106 | ) | |
107 | }, | |
108 | ||
109 | err => this.error = err.message | |
110 | ) | |
111 | } | |
112 | ||
8094a898 C |
113 | private onUserFetched (userJson: User) { |
114 | this.userId = userJson.id | |
115 | this.username = userJson.username | |
328c78bc | 116 | this.userEmail = userJson.email |
8094a898 C |
117 | |
118 | this.form.patchValue({ | |
119 | email: userJson.email, | |
954605a8 | 120 | role: userJson.role, |
bee0abff FA |
121 | videoQuota: userJson.videoQuota, |
122 | videoQuotaDaily: userJson.videoQuotaDaily | |
8094a898 C |
123 | }) |
124 | } | |
125 | } |