]>
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' |
1eddc9a7 | 13 | import { 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 | }) |
20 | export 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, |
e309822b | 32 | private userValidatorsService: UserValidatorsService, |
8094a898 C |
33 | private route: ActivatedRoute, |
34 | private router: Router, | |
f8b2c1b4 | 35 | private notifier: Notifier, |
b1d40cff C |
36 | private userService: UserService, |
37 | private i18n: I18n | |
8094a898 C |
38 | ) { |
39 | super() | |
3827c3b3 C |
40 | |
41 | this.buildQuotaOptions() | |
8094a898 C |
42 | } |
43 | ||
8094a898 | 44 | ngOnInit () { |
bee0abff | 45 | const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' } |
d18d6478 | 46 | this.buildForm({ |
e309822b C |
47 | email: this.userValidatorsService.USER_EMAIL, |
48 | role: this.userValidatorsService.USER_ROLE, | |
bee0abff | 49 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, |
1eddc9a7 C |
50 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY, |
51 | byPassAutoBlacklist: null | |
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 | }) | |
62 | } | |
63 | ||
64 | ngOnDestroy () { | |
65 | this.paramsSub.unsubscribe() | |
66 | } | |
67 | ||
68 | formValidated () { | |
69 | this.error = undefined | |
70 | ||
71 | const userUpdate: UserUpdate = this.form.value | |
1eddc9a7 | 72 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) |
8094a898 C |
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) | |
bee0abff | 76 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
8094a898 C |
77 | |
78 | this.userService.updateUser(this.userId, userUpdate).subscribe( | |
79 | () => { | |
f8b2c1b4 | 80 | this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username })) |
8094a898 C |
81 | this.router.navigate([ '/admin/users/list' ]) |
82 | }, | |
83 | ||
f7354483 | 84 | err => this.error = err.message |
8094a898 C |
85 | ) |
86 | } | |
87 | ||
88 | isCreation () { | |
89 | return false | |
90 | } | |
91 | ||
92 | getFormButtonTitle () { | |
b1d40cff | 93 | return this.i18n('Update user') |
8094a898 C |
94 | } |
95 | ||
328c78bc RK |
96 | resetPassword () { |
97 | this.userService.askResetPassword(this.userEmail).subscribe( | |
98 | () => { | |
b426edd4 | 99 | this.notifier.success( |
328c78bc RK |
100 | this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username }) |
101 | ) | |
102 | }, | |
103 | ||
104 | err => this.error = err.message | |
105 | ) | |
106 | } | |
107 | ||
8094a898 C |
108 | private onUserFetched (userJson: User) { |
109 | this.userId = userJson.id | |
110 | this.username = userJson.username | |
328c78bc | 111 | this.userEmail = userJson.email |
8094a898 C |
112 | |
113 | this.form.patchValue({ | |
114 | email: userJson.email, | |
954605a8 | 115 | role: userJson.role, |
bee0abff | 116 | videoQuota: userJson.videoQuota, |
1eddc9a7 C |
117 | videoQuotaDaily: userJson.videoQuotaDaily, |
118 | byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST | |
8094a898 C |
119 | }) |
120 | } | |
121 | } |