]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Subscription } from 'rxjs' |
b426edd4 | 2 | import { Component, OnDestroy, OnInit } from '@angular/core' |
8094a898 | 3 | import { ActivatedRoute, Router } from '@angular/router' |
3827c3b3 | 4 | import { ConfigService } from '@app/+admin/config/shared/config.service' |
67ed6552 C |
5 | import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core' |
6 | import { FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms' | |
7 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
8 | import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models' | |
9 | import { UserEdit } from './user-edit' | |
8094a898 C |
10 | |
11 | @Component({ | |
12 | selector: 'my-user-update', | |
6a84aafd C |
13 | templateUrl: './user-edit.component.html', |
14 | styleUrls: [ './user-edit.component.scss' ] | |
8094a898 C |
15 | }) |
16 | export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy { | |
17 | error: string | |
8094a898 | 18 | |
8094a898 C |
19 | private paramsSub: Subscription |
20 | ||
21 | constructor ( | |
d18d6478 | 22 | protected formValidatorService: FormValidatorService, |
6a84aafd | 23 | protected serverService: ServerService, |
3827c3b3 | 24 | protected configService: ConfigService, |
76314386 | 25 | protected screenService: ScreenService, |
a95a4cc8 | 26 | protected auth: AuthService, |
e309822b | 27 | private userValidatorsService: UserValidatorsService, |
8094a898 C |
28 | private route: ActivatedRoute, |
29 | private router: Router, | |
f8b2c1b4 | 30 | private notifier: Notifier, |
b1d40cff C |
31 | private userService: UserService, |
32 | private i18n: I18n | |
8094a898 C |
33 | ) { |
34 | super() | |
3827c3b3 C |
35 | |
36 | this.buildQuotaOptions() | |
8094a898 C |
37 | } |
38 | ||
8094a898 | 39 | ngOnInit () { |
ba430d75 C |
40 | super.ngOnInit() |
41 | ||
76314386 RK |
42 | const defaultValues = { |
43 | role: UserRole.USER.toString(), | |
44 | videoQuota: '-1', | |
45 | videoQuotaDaily: '-1' | |
46 | } | |
47 | ||
d18d6478 | 48 | this.buildForm({ |
e309822b C |
49 | email: this.userValidatorsService.USER_EMAIL, |
50 | role: this.userValidatorsService.USER_ROLE, | |
bee0abff | 51 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, |
1eddc9a7 | 52 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY, |
3487330d | 53 | byPassAutoBlock: null |
d18d6478 | 54 | }, defaultValues) |
8094a898 C |
55 | |
56 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
57 | const userId = routeParams['id'] | |
76314386 | 58 | this.userService.getUser(userId, true).subscribe( |
8094a898 C |
59 | user => this.onUserFetched(user), |
60 | ||
f7354483 | 61 | err => this.error = err.message |
8094a898 C |
62 | ) |
63 | }) | |
64 | } | |
65 | ||
66 | ngOnDestroy () { | |
67 | this.paramsSub.unsubscribe() | |
68 | } | |
69 | ||
70 | formValidated () { | |
71 | this.error = undefined | |
72 | ||
73 | const userUpdate: UserUpdate = this.form.value | |
1eddc9a7 | 74 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) |
8094a898 C |
75 | |
76 | // A select in HTML is always mapped as a string, we convert it to number | |
77 | userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
bee0abff | 78 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
8094a898 | 79 | |
76314386 | 80 | this.userService.updateUser(this.user.id, userUpdate).subscribe( |
8094a898 | 81 | () => { |
86521a67 | 82 | this.notifier.success(this.i18n('User {{username}} updated.', { username: this.user.username })) |
8094a898 C |
83 | this.router.navigate([ '/admin/users/list' ]) |
84 | }, | |
85 | ||
f7354483 | 86 | err => this.error = err.message |
8094a898 C |
87 | ) |
88 | } | |
89 | ||
90 | isCreation () { | |
91 | return false | |
92 | } | |
93 | ||
45f1bd72 JL |
94 | isPasswordOptional () { |
95 | return false | |
96 | } | |
97 | ||
8094a898 | 98 | getFormButtonTitle () { |
b1d40cff | 99 | return this.i18n('Update user') |
8094a898 C |
100 | } |
101 | ||
328c78bc | 102 | resetPassword () { |
76314386 | 103 | this.userService.askResetPassword(this.user.email).subscribe( |
328c78bc | 104 | () => { |
b426edd4 | 105 | this.notifier.success( |
76314386 | 106 | this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.user.username }) |
328c78bc RK |
107 | ) |
108 | }, | |
109 | ||
110 | err => this.error = err.message | |
111 | ) | |
112 | } | |
113 | ||
76314386 RK |
114 | private onUserFetched (userJson: UserType) { |
115 | this.user = new User(userJson) | |
8094a898 C |
116 | |
117 | this.form.patchValue({ | |
118 | email: userJson.email, | |
76314386 | 119 | role: userJson.role.toString(), |
bee0abff | 120 | videoQuota: userJson.videoQuota, |
1eddc9a7 | 121 | videoQuotaDaily: userJson.videoQuotaDaily, |
3487330d | 122 | byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST |
8094a898 C |
123 | }) |
124 | } | |
125 | } |