]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnDestroy, OnInit, Input } from '@angular/core' | |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { Subscription } from 'rxjs' | |
4 | import { Notifier } from '@app/core' | |
5 | import { ServerService } from '../../../core' | |
6 | import { UserEdit } from './user-edit' | |
7 | import { User, UserUpdate } from '../../../../../../shared' | |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
9 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
10 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' | |
11 | import { ConfigService } from '@app/+admin/config/shared/config.service' | |
12 | import { UserService } from '@app/shared' | |
13 | ||
14 | @Component({ | |
15 | selector: 'my-user-update', | |
16 | templateUrl: './user-edit.component.html', | |
17 | styleUrls: [ './user-edit.component.scss' ] | |
18 | }) | |
19 | export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy { | |
20 | error: string | |
21 | userId: number | |
22 | userEmail: string | |
23 | username: string | |
24 | ||
25 | private paramsSub: Subscription | |
26 | ||
27 | constructor ( | |
28 | protected formValidatorService: FormValidatorService, | |
29 | protected serverService: ServerService, | |
30 | protected configService: ConfigService, | |
31 | private userValidatorsService: UserValidatorsService, | |
32 | private route: ActivatedRoute, | |
33 | private router: Router, | |
34 | private notifier: Notifier, | |
35 | private userService: UserService, | |
36 | private i18n: I18n | |
37 | ) { | |
38 | super() | |
39 | ||
40 | this.buildQuotaOptions() | |
41 | } | |
42 | ||
43 | ngOnInit () { | |
44 | const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' } | |
45 | this.buildForm({ | |
46 | email: this.userValidatorsService.USER_EMAIL, | |
47 | role: this.userValidatorsService.USER_ROLE, | |
48 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, | |
49 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY | |
50 | }, defaultValues) | |
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 | ||
57 | err => this.error = err.message | |
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) | |
73 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) | |
74 | ||
75 | this.userService.updateUser(this.userId, userUpdate).subscribe( | |
76 | () => { | |
77 | this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username })) | |
78 | this.router.navigate([ '/admin/users/list' ]) | |
79 | }, | |
80 | ||
81 | err => this.error = err.message | |
82 | ) | |
83 | } | |
84 | ||
85 | isCreation () { | |
86 | return false | |
87 | } | |
88 | ||
89 | getFormButtonTitle () { | |
90 | return this.i18n('Update user') | |
91 | } | |
92 | ||
93 | resetPassword () { | |
94 | this.userService.askResetPassword(this.userEmail).subscribe( | |
95 | () => { | |
96 | this.notificationsService.success( | |
97 | this.i18n('Success'), | |
98 | this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username }) | |
99 | ) | |
100 | }, | |
101 | ||
102 | err => this.error = err.message | |
103 | ) | |
104 | } | |
105 | ||
106 | private onUserFetched (userJson: User) { | |
107 | this.userId = userJson.id | |
108 | this.username = userJson.username | |
109 | this.userEmail = userJson.email | |
110 | ||
111 | this.form.patchValue({ | |
112 | email: userJson.email, | |
113 | role: userJson.role, | |
114 | videoQuota: userJson.videoQuota, | |
115 | videoQuotaDaily: userJson.videoQuotaDaily | |
116 | }) | |
117 | } | |
118 | } |